|
|
|
|
@ -12,64 +12,16 @@ public class Main {
|
|
|
|
|
|
|
|
|
|
//Constructor, not the java main
|
|
|
|
|
public Main(String[] args) {
|
|
|
|
|
//init options, it will load defaults
|
|
|
|
|
//from resource xml
|
|
|
|
|
//options = new Options();
|
|
|
|
|
System.out.println("Welcome to " + PROGRAM_NAME + "!");
|
|
|
|
|
guess();
|
|
|
|
|
end();
|
|
|
|
|
//
|
|
|
|
|
//parse args, uses
|
|
|
|
|
//options.parseArgs(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// java main
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
new Main(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void guess() {
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
while (true) {
|
|
|
|
|
int range = rand.nextInt(1000);
|
|
|
|
|
int secret = rand.nextInt(range);
|
|
|
|
|
System.out.println("Can you guess my number? [0 - " + range + "]");
|
|
|
|
|
System.out.println("Enter -1 to end");
|
|
|
|
|
int user = getUserInt();
|
|
|
|
|
if (user < 0) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
guessResult(user, secret);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getUserInt() {
|
|
|
|
|
Scanner scan = new Scanner(System.in);
|
|
|
|
|
int result = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (scan.hasNextInt()) {
|
|
|
|
|
result = scan.nextInt();
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("Try again?");
|
|
|
|
|
scan.nextLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void guessResult(int user, int secret) {
|
|
|
|
|
if (user < secret || user > secret) {
|
|
|
|
|
this.games++;
|
|
|
|
|
System.out.println("Too bad. You guessed "
|
|
|
|
|
+ secret + " with " + user + ".");
|
|
|
|
|
} else {
|
|
|
|
|
this.games++;
|
|
|
|
|
this.win++;
|
|
|
|
|
System.out.println("Wonderful! You have correctly guessed: "
|
|
|
|
|
+ secret + " with " + user + "!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void end() {
|
|
|
|
|
System.out.println("You have won: " + this.win + " out of " + this.games
|
|
|
|
|
+ "!");
|
|
|
|
|
System.out.println("Thank you for playing this game! Bye!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|