Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Guess {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         printBeginning();
  8.        
  9.         boolean playAgain = true;
  10.         while (playAgain) {
  11.             game();
  12.             System.out.print("Do you want to play again? ");
  13.             playAgain = scanner.nextLine().equals("Yes");
  14.         }
  15.     }
  16.    
  17.     private static void printBeginning() {
  18.         System.out.println("This program allows you to play a guessing game");
  19.     }
  20.    
  21.     private static void game() {
  22.         Random rnd = new Random();
  23.         Scanner scanner = new Scanner(System.in);
  24.         int correctNum = rnd.nextInt(100);
  25.         int guesses = 0;
  26.         int guessedNum;
  27.        
  28.         System.out.println("I'm thinking of a number...");
  29.         while (correctNum != guessedNum) {
  30.             System.out.print("Your guess? ");
  31.             guessedNum = scanner.nextInt();
  32.            
  33.             if (guessedNum > correctNum) {
  34.                 System.out.println("higher");
  35.             } else if (guessedNum < correectNum) {
  36.                 System.out.println("lower");
  37.             }
  38.         }
  39.        
  40.         System.out.println("You got it right in " + guesses + " guesses");
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement