Advertisement
AngelKejov

Number guessing game!

Apr 26th, 2021
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Exersices;
  2.  
  3. import java.util.*;
  4.  
  5. public class NumberGuessingGame {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         System.out.println("/////////////////////");
  10.         System.out.println("NUMBER GUESSING GAME!");
  11.         System.out.println("/////////////////////\n");
  12.  
  13.         System.out.println("**************************************************");
  14.         System.out.println("         RULES:");
  15.         System.out.println("We have selected a random number \n" +
  16.                 "between 1 and 100. See if you can guess it in \n" +
  17.                 "10 turns or fewer. We'll tell you if your \n" +
  18.                 "guess was too high or too low.");
  19.         System.out.println("**************************************************\n");
  20.  
  21.         System.out.print("Enter a guess: ");
  22.         int n = Integer.parseInt(sc.nextLine());
  23.  
  24.         int lives = 10;
  25.         Random rand = new Random();
  26.         int rand_int1 = rand.nextInt(100);
  27.  
  28.         List<Integer> previousGuesses = new ArrayList<>();
  29.         while (true) {
  30.             if (n == rand_int1) {
  31.                 System.out.println("You win!");
  32.                 break;
  33.             } else {
  34.                 System.out.println("Try again!");
  35.                 System.out.print("Enter a guess: ");
  36.                 n = Integer.parseInt(sc.nextLine());
  37.                 if (n < rand_int1) {
  38.                     System.out.println("Last guess was too low!");
  39.                 } else if (n > rand_int1) {
  40.                     System.out.println("Last guess was too high!");
  41.                 }
  42.                 lives--;
  43.  
  44.                 previousGuesses.add(n);
  45.  
  46.                 if (lives == 0) {
  47.                     System.out.println();
  48.                     System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
  49.                     System.out.println("!!!GAME OVER!!!");
  50.                     System.out.printf("The number was %d.%n", rand_int1);
  51.                     System.out.print("You guesses was: ");
  52.                     for (Integer previousGuess : previousGuesses) {
  53.                         System.out.print(previousGuess + " ");
  54.                     }
  55.                     System.out.println();
  56.                     System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
  57.                     break;
  58.                 }
  59.             }
  60.  
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement