Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.util.Random; // import random library
  2. import java.util.Scanner; // import user input library
  3.  
  4.  //**Ben Pacheco's AP Computer Science A Final Project. I created a random number game utilizing a good bit of the material that we learned on through the Edhesive program. Created on 1/18/2018.**
  5.  
  6. class Main {
  7.     public static void main(String[] args) {
  8.  
  9.         // variables
  10.         int max;
  11.         int guess;
  12.         int answer;
  13.         boolean win;
  14.  
  15.         // init scanners
  16.         Scanner inpt = new Scanner(System.in);
  17.         Random randy = new Random();
  18.  
  19.         System.out.println("Choose a max number: ");
  20.  
  21.         // get user input for max number
  22.         max = inpt.nextInt();
  23.  
  24.         // get random answer
  25.         answer = randy.nextInt(max);
  26.  
  27.         // initialize win status to false
  28.         win = false;
  29.  
  30.         // game loop
  31.         while (win == false) {
  32.  
  33.             System.out.println("Guess a number between 1 and " + max);
  34.             guess = inpt.nextInt(); // get user's guess
  35.  
  36.             if (guess == answer) {
  37.                 win = true;
  38.                 System.out.println("You won!");
  39.  
  40.             } else if (guess < answer)
  41.                 System.out.println("Your guess is < the answer!");
  42.  
  43.             else if (guess > answer)
  44.                 System.out.println("Your guess is > the answer!");
  45.  
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement