{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Guess - guess = scan.nextInt numGuesses = 1 tooHigh = 0...

This preview shows pages 1–2. Sign up to view the full content.

// **************************************************************** // Guess.java // // Play a game where the user guesses a number from 1 to 10 // // **************************************************************** import java.util.Scanner; import java.util.Random; public class Guess { public static void main(String[] args) { int numToGuess; //Number the user tries to guess int guess; //The user's guess int numGuesses; //Number of guesses int tooHigh; //Number of too high guesses int tooLow; //Number of too low guesses Random generator = new Random(); Scanner scan = new Scanner (System.in); //randomly generate the number to guess numToGuess = generator.nextInt(10) + 1; //print message asking user to enter a guess System.out.println("I'm thinking of a number!!"); System.out.print("Enter your guess: "); //read in guess

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: guess = scan.nextInt(); numGuesses = 1; tooHigh = 0; tooLow = 0; while (guess != numToGuess ) //keep going as long as the guess is wrong { numGuesses++; if (guess > numToGuess) { System.out.print("Too high, guess again: "); tooHigh++; } else { System.out.print("Too low, guess again: "); tooLow++; } guess = scan.nextInt(); } if (numGuesses == 1) System.out.println ("You got it, after only one guess!!"); else System.out.println("You got it, after only " + numGuesses + " guesses!!"); if (tooHigh == 1) System.out.println(tooHigh + " guess was too high."); else System.out.println(tooHigh + " guesses were too high."); if (tooLow == 1) System.out.println(tooLow + " guess was too low."); else System.out.println(tooLow + " guesses were too low."); } }...
View Full Document

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern