JordanFarnell

Guessing Game

Oct 19th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package guessinggame;
  2. import java.util.Scanner;
  3. public class GuessingGame {
  4.  
  5.     public static void giveHighHint() {
  6.         System.out.println("Guess a higher number.");
  7.     }
  8.    
  9.     public static void giveLowHint() {
  10.         System.out.println("Guess a lower number.");
  11.     }
  12.    
  13.     public static int randomNumber(int randomNum) {
  14.         randomNum = (int)(20 * Math.random() + 1);
  15.         return(randomNum);
  16.     }
  17.    
  18.     public static void main(String[] args) {
  19.         int randomNum = 0, guess = 0;
  20.         Scanner input = new Scanner(System.in);
  21.         randomNum = randomNumber(randomNum);
  22.         while (guess != randomNum) {
  23.             System.out.print("Guess a number between 1 & 20: ");
  24.             guess = input.nextInt();
  25.             if (guess > randomNum) {
  26.                 giveLowHint();
  27.             }
  28.             else if (guess < randomNum) {
  29.                 giveHighHint();
  30.             }
  31.         }
  32.         System.out.print("You guessed the right number!");
  33.     }
  34. }
Add Comment
Please, Sign In to add comment