Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Contains a for loop, that puts guesses into a guesses array, in that same for loop there is another for loop once the index
- * reaches 2. That loop then displays the random number chosen and displays guesses, if the guess is equal to the random number
- * chosen "Winner" is displayed if it is not "Sorry, Wrong" is displayed. If you press 1 and choose to play again index is set
- * to 0, and the for loop restarts choosing a different random number for the game.
- *
- * @author cstecschoolgithub
- * Date : 1/17/19
- *
- */
- import java.util.*;
- public class guessingGame {
- public static void main(String [] args)
- {
- Random random = new Random();
- Scanner scan = new Scanner(System.in);
- int playAgain = 0;
- int guessInt = 0;
- int rnd = random.nextInt(10) + 1;
- int[] guesses = new int [3];
- int guess = 0;
- int guess2 = 0;
- System.out.println("This is the one and only guessing game");
- System.out.println("Please guess a number between 1 and 10");
- for (int index = 0; index < 3; index++)
- {
- if (playAgain == 1)
- {
- index = index - 1;
- rnd = random.nextInt(10) + 1;
- playAgain = 2;
- }
- if(guess == 3)
- {
- guess = 0;
- }
- guess = guess + 1;
- System.out.print("Enter guess " + guess + ":");
- try
- {
- guessInt = Integer.parseInt(scan.nextLine());
- }
- catch (NumberFormatException e)
- {
- System.out.println("Please Enter An Integer Value For Your Guess");
- guessInt = Integer.parseInt(scan.nextLine());
- }
- guesses[index] = guessInt;
- while(guessInt < 1 || guessInt > 10 && bool == true)
- {
- System.out.println("Invalid. Re-enter guess. ");
- guessInt = Integer.parseInt(scan.nextLine());
- guesses[index] = guessInt;
- }
- if (index == 2)
- {
- System.out.println("The Random Number Chosen Was: " + rnd + "!");
- for (int index2 = 0; index2 < 3; index2++)
- {
- if(guess2 == 3)
- {
- guess2 = 0;
- }
- guess2 = guess2 + 1;
- if (guesses[index2] == rnd)
- {
- System.out.println("Guess: " + guess2 + " was " + guesses[index2]);
- System.out.println("Winner");
- }
- else
- {
- System.out.println("Guess: " + guess2 + " was " + guesses[index2]);
- System.out.println("Sorry, Wrong");
- }
- if (index2 == 2)
- {
- System.out.println("Enter Either 1 To Play Again or 2 To Quit");
- try
- {
- playAgain = Integer.parseInt(scan.nextLine());
- }
- catch (NumberFormatException e)
- {
- System.out.println("Please Enter An Integer Other Than 1 to Quit The Game or 1 to Restart!");
- playAgain = Integer.parseInt(scan.nextLine());
- }
- if(playAgain < 1 || playAgain > 2)
- {
- System.out.println("Please Enter An Integer Other Than 1 to Quit The Game or 1 to Restart!");
- playAgain = Integer.parseInt(scan.nextLine());
- }
- if (playAgain == 1)
- {
- index = 0;
- }
- else
- {
- System.out.println("The Game Has Ended!");
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment