Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ryan Blume
- //10/11/19
- //Guessing Game
- import javax.swing.*;
- public class GuessingGame
- {
- public static void main (String[] args)
- {
- int i=1; //i will be used to determine whether or not the game will be replayed
- do
- {
- int ans = Integer.parseInt(JOptionPane.showInputDialog("Player 1 enter a number between 1 and 1000"));
- int x = 1;
- int tries = 1;
- do
- {
- int guess = Integer.parseInt(JOptionPane.showInputDialog("Player 2 enter your guess"));
- if (guess == ans)
- {
- x ++; //increasing x from one to two will end the game
- JOptionPane.showMessageDialog(null, "Correct! It took you " + tries + " tries to guess the correct answer!");
- }
- else
- {
- tries++;
- if(guess>ans)
- {
- System.out.println("Incorrect: The answer is LOWER than " + guess);
- }
- if(guess<ans)
- {
- System.out.println("Incorrect: The answer is HIGHER than " + guess);
- }
- }
- }while (x==1);
- String s = JOptionPane.showInputDialog("Would you like to play again? (Yes/No)");
- System.out.println(s);
- if (s == "no")
- {
- i++;
- }
- }while (i==1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement