Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random; // import random library
- import java.util.Scanner; // import user input library
- //**Ben Pacheco's AP Computer Science A Final Project. I created a random number guessing game utilizing a good bit of the material that we learned on through the Edhesive program.**
- class Main {
- public static void main(String[] args) {
- // variables
- int max;
- int guess;
- int answer;
- boolean win;
- // init scanners
- Scanner inpt = new Scanner(System.in);
- Random randy = new Random();
- System.out.println("Choose a maximum number: ");
- // get user input for max number
- max = inpt.nextInt();
- // get random answer
- answer = randy.nextInt(max);
- // initialize win status to false
- win = false;
- // game loop
- while (win == false) {
- System.out.println("Guess a number between 1 and " + max);
- guess = inpt.nextInt(); // get user's guess
- if (guess == answer) {
- win = true;
- System.out.println("You won!");
- } else if (guess < answer)
- System.out.println("Your guess is < the answer!"); //alerts the user their number is less than the answer
- else if (guess > answer)
- System.out.println("Your guess is > the answer!"); //alerts the user their number is greater than the answer
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement