Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unfortunetly, more while statement problems...

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
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:

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(&quot;\n\n&quot; + won + &quot;\n\n\n&quot;);
				runs = runsIntended + 1;
			}

			else {
				if (guess < number) {
					System.out.print(&quot;\n\n&quot; + hi + &quot;\n&quot; + prompt);
					guess = keyboard.readInt();
				}

				else {

					if (guess > number) {
						System.out.print(&quot;\n\n&quot; + lo + &quot;\n&quot; + prompt);
						guess = keyboard.readInt();
					}
				}
			}

			runs++;

			if ((runs == runsIntended) && (win == 0)) {
				System.out.print(&quot;\n\n&quot; + lost + &quot;\n\n\n&quot;);
				runs = runsIntended + 1;
			}
		}
	}
}

Thanks for helping me! &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Update on the code...

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(&quot;Enter the highest possible number to place &quot;
		+ &quot;guesses between: &quot;);
		int n = keyboard.readInt();

		int number = generator.nextInt(n);
		int runs = 1;
		int win = 0;
		int runsIntended = (int)(Math.floor(Math.log(n) / Math.log(2)));
		String hi = &quot;Higher&quot;;
		String lo = &quot;Lower&quot;;
		String won = &quot;You win!&quot;;
		String lost = &quot;You Lose.\nI was thinking of &quot; + number + &quot;.&quot;;
		String prompt = &quot;Guess my number: &quot;;

		System.out.print(&quot;\n\nI'm thinking of a number between 1 and &quot;
		+ n + &quot;.\nYou have &quot; + runsIntended + &quot; guesses.\n&quot; + prompt);
		int guess = keyboard.readInt();

		while (runs <= runsIntended) {

			if (guess == number) {
				System.out.print(&quot;\n\n&quot; + won + &quot;\n\n\n&quot;);
				runs = runsIntended + 1;
				win = 1;
			}

			else {
				if (guess < number) {
					System.out.print(&quot;\n\n&quot; + hi + &quot;\n&quot; + prompt);
					guess = keyboard.readInt();
				}

				else {

					if (guess > number) {
						System.out.print(&quot;\n\n&quot; + lo + &quot;\n&quot; + prompt);
						guess = keyboard.readInt();
					}
				}
			}

			runs++;

			if ((runs == runsIntended) && (win == 0)) {
				System.out.print(&quot;\n\n&quot; + lost + &quot;\n\n\n&quot;);
				runs = runsIntended + 1;
			}
		}
	}
}
X-) &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
<grumble>Where's those smart people when you need them?</grumble> :-Q &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top