SHARE
TWEET

Untitled

a guest Jan 18th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *
  3. * Contains a for loop, that puts guesses into a guesses array, in that same for loop there is another for loop once the index
  4. * reaches 2. That loop then displays the random number chosen and displays guesses, if the guess is equal to the random number
  5. * chosen "Winner" is displayed if it is not "Sorry, Wrong" is displayed. If you press 1 and choose to play again index is set
  6. * to 0, and the for loop restarts choosing a different random number for the game.
  7. *
  8. * @author cstecschoolgithub
  9. * Date : 1/17/19
  10. *
  11. */
  12. import java.util.*;
  13. public class guessingGame {
  14.     public static void main(String [] args)
  15.     {
  16. Random random = new Random();
  17. Scanner scan = new Scanner(System.in);
  18. int playAgain = 0;
  19. int guessInt = 0;
  20. int rnd = random.nextInt(10) + 1;
  21. int[] guesses = new int [3];
  22. int guess = 0;
  23. int guess2 = 0;
  24. System.out.println("This is the one and only guessing game");
  25. System.out.println("Please guess a number between 1 and 10");
  26. for (int index = 0; index < 3; index++)
  27. {
  28.     if (playAgain == 1)
  29.     {
  30.         index = index - 1;
  31.         rnd = random.nextInt(10) + 1;
  32.         playAgain = 2;
  33.     }
  34.     if(guess == 3)
  35.     {
  36.         guess = 0;
  37.     }
  38.     guess = guess + 1;
  39.     System.out.print("Enter guess " + guess + ":");
  40.     try
  41.     {
  42.         guessInt = Integer.parseInt(scan.nextLine());
  43.     }
  44.     catch (NumberFormatException e)
  45.     {
  46.         System.out.println("Please Enter An Integer Value For Your Guess");
  47.         guessInt = Integer.parseInt(scan.nextLine());
  48.     }
  49.    guesses[index] = guessInt;
  50.         while(guessInt < 1 || guessInt > 10 && bool == true)
  51.         {
  52.             System.out.println("Invalid. Re-enter guess. ");
  53.                 guessInt = Integer.parseInt(scan.nextLine());
  54.             guesses[index] = guessInt;
  55.         }
  56. if (index == 2)
  57. {
  58.     System.out.println("The Random Number Chosen Was: " + rnd + "!");
  59. for (int index2 = 0; index2 < 3; index2++)
  60. {
  61.     if(guess2 == 3)
  62.     {
  63.         guess2 = 0;
  64.     }
  65.     guess2 = guess2 + 1;
  66.     if (guesses[index2] == rnd)
  67.     {
  68.         System.out.println("Guess: "  + guess2 + " was " + guesses[index2]);
  69.         System.out.println("Winner");
  70.     }
  71.     else
  72.     {
  73.         System.out.println("Guess: "  + guess2 + " was " + guesses[index2]);
  74.         System.out.println("Sorry, Wrong");
  75.     }
  76.     if (index2 == 2)
  77.     {
  78.         System.out.println("Enter Either 1 To Play Again or 2 To Quit");
  79.        
  80.           try
  81.             {
  82.                 playAgain = Integer.parseInt(scan.nextLine());
  83.             }
  84.             catch (NumberFormatException e)
  85.             {
  86.                 System.out.println("Please Enter An Integer Other Than 1 to Quit The Game or 1 to Restart!");
  87.                 playAgain = Integer.parseInt(scan.nextLine());
  88.             }
  89.           if(playAgain < 1 || playAgain > 2)
  90.           {
  91.               System.out.println("Please Enter An Integer Other Than 1 to Quit The Game or 1 to Restart!");
  92.                 playAgain = Integer.parseInt(scan.nextLine());
  93.           }
  94.         if (playAgain == 1)
  95.         {
  96.             index = 0;
  97.         }
  98.         else
  99.         {
  100.             System.out.println("The Game Has Ended!");
  101.         }
  102.     }
  103. }
  104. }
  105.   }
  106.     }
  107.                         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top