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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about while loops...

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
0
0
US
I can't remember what the symbol for AND is in a loop. I tried && and || but they didn't work. "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
 
&& is used for logical ANDs and || is used for logical ORs. Post your code, it is probably a mistake in usage (most likely parens).
 
I needed && X-), but I have another problem :cool:, so here is my code:

Code:
import java.util.*;

public class Guess {

	public static void main(String[] args) {
		ConsoleReader keyboard = new ConsoleReader(System.in);
		Random generator = new Random();

		int number = generator.nextInt(100);
		int runs = 1;
		int win = 0;
		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 "
		+ "100.\n" + prompt);
		int guess = keyboard.readInt();

		while (runs <= 6) {

			if (guess == number) {
				System.out.print(&quot;\n\n&quot; + won + &quot;\n\n\n&quot;);
				runs = 7;
				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 = runs + 1;
			if (runs == 6 && win == 0) {
				System.out.print(&quot;\n\n&quot; + lost + &quot;\n\n\n&quot;);
			}
		}
	}
}
&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