Skip to content

Instantly share code, notes, and snippets.

@tgruber5150
Created September 10, 2012 23:43
Show Gist options
  • Save tgruber5150/3694865 to your computer and use it in GitHub Desktop.
Save tgruber5150/3694865 to your computer and use it in GitHub Desktop.
Pick a number: You get 5 changes to pick the right number, then you're kicked out of the program!
import java.util.Scanner;
public class PickANumber {
private static int a = (int) (Math.random() * 10 + 1);
public static void main (String[] args) {
for (int i = 0; i < 5; ++i) {
System.out.print("Pick a number from 1 to 10: ");
Scanner in = new Scanner (System.in);
int b = Integer.parseInt(in.next());
if (b == a) {
System.out.println("Good Guess!");
printSwitchStatement();
break;
}
else {System.out.println("Nope!");
if (i == 4) System.out.println("Now you're just guessing!");
}
}
}
private static void printSwitchStatement() {
switch(a) {
case 1: System.out.println("1 is a lonely number, but not today!");
break;
case 2: System.out.println("2 it is! Yoda");
break;
case 3: System.out.println("3 is a magic number!");
break;
case 4: System.out.println("4. Score!");
break;
case 5: System.out.println("5 is a prime number you know!");
break;
case 6: System.out.println("6 it is! Yoda");
break;
case 7: System.out.println("7. It's like we're connected!");
break;
case 8: System.out.println("8; the snowman!");
break;
case 9: System.out.println("9; unless you're looking at it upside down...");
break;
case 10: System.out.println("10; the first double-digit number in the positive scale!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment