GT500FOMOCO
Programmer
I have no idea what it is. As far as I can tell, it should work. It seems to like to skip most of the statement on the last run and tell you that you lost, even if you won. Here is the code:
Thanks for helping me! "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse
"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
Code:
import java.util.*;
public class Guess {
public static void main(String[] args) {
ConsoleReader keyboard = new ConsoleReader(System.in);
Random generator = new Random();
System.out.print("Enter the highest possible number to place "
+ "guesses between: ");
int n = keyboard.readInt();
int number = generator.nextInt(n);
int runs = 1;
int win = 1;
int runsIntended = (int)(Math.floor(Math.log(n) / Math.log(2)));
String hi = "Higher";
String lo = "Lower";
String won = "You win!";
String lost = "You Lose.\nI was thinking of " + number + ".";
String prompt = "Guess my number: ";
System.out.print("\n\nI'm thinking of a number between 1 and "
+ n + ".\nYou have " + runsIntended + " guesses.\n" + prompt);
int guess = keyboard.readInt();
while (runs <= runsIntended) {
if (guess == number) {
System.out.print("\n\n" + won + "\n\n\n");
runs = runsIntended + 1;
}
else {
if (guess < number) {
System.out.print("\n\n" + hi + "\n" + prompt);
guess = keyboard.readInt();
}
else {
if (guess > number) {
System.out.print("\n\n" + lo + "\n" + prompt);
guess = keyboard.readInt();
}
}
}
runs++;
if ((runs == runsIntended) && (win == 0)) {
System.out.print("\n\n" + lost + "\n\n\n");
runs = runsIntended + 1;
}
}
}
}
Thanks for helping me! "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse
"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy