Advertisement
Zappline

GuessMyNumber

Sep 15th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class GuessMyNumber{
  6.  
  7.     public static void main(String[] args) {
  8.        
  9.         int guess;
  10.         Random rand = new Random();    
  11.         Scanner scanner = new Scanner(System.in);
  12.         boolean win = false;
  13.         Scanner ans = new Scanner(System.in);
  14.         boolean play = true;
  15.        
  16.         while (play == true){
  17.             int numberOfTries = 0;
  18.             int numberToGuess = (rand.nextInt(100)+1);
  19.             win = false;               
  20.            
  21.         while (win == false){
  22.                        
  23.             System.out.println("What number am I thinking off? \n Tip: it's between 1 and 100");
  24.             guess = scanner.nextInt();
  25.             numberOfTries++;
  26.             play = false;
  27.            
  28.             if (guess < numberToGuess){
  29.                 System.out.println("Your guess is to low, try again");
  30.            
  31.             } else if (guess > numberToGuess){
  32.                 System.out.println("Your guess is to high, try again");
  33.            
  34.             } else if (guess == numberToGuess){
  35.                 win = true;
  36.                 System.out.println("Congratulations, you where correct and it only took you " + numberOfTries + " tries");
  37.                 System.out.println("Would you like to play again? (y/n)");
  38.                 String answer = ans.nextLine();
  39.                
  40.                 if (answer.equalsIgnoreCase("y")) {
  41.                     play = true;
  42.                                                            
  43.                 } else if (answer.equalsIgnoreCase("n")){
  44.                     System.out.println("Thank you for playing!");
  45.                     scanner.close();
  46.                     ans.close();
  47.                     break;
  48.                
  49.                 }
  50.                                
  51.             }
  52.                        
  53.         }
  54.     }          
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement