Advertisement
ryanblume11

GuessingGameProgram

Oct 14th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. //Ryan Blume
  2. //10/11/19
  3. //Guessing Game
  4. import javax.swing.*;
  5. public class GuessingGame
  6. {
  7. public static void main (String[] args)
  8. {
  9. int i=1; //i will be used to determine whether or not the game will be replayed
  10. do
  11. {
  12.  
  13. int ans = Integer.parseInt(JOptionPane.showInputDialog("Player 1 enter a number between 1 and 1000"));
  14. int x = 1;
  15. int tries = 1;
  16. do
  17. {
  18. int guess = Integer.parseInt(JOptionPane.showInputDialog("Player 2 enter your guess"));
  19. if (guess == ans)
  20. {
  21. x ++; //increasing x from one to two will end the game
  22. JOptionPane.showMessageDialog(null, "Correct! It took you " + tries + " tries to guess the correct answer!");
  23. }
  24. else
  25. {
  26. tries++;
  27. if(guess>ans)
  28. {
  29. System.out.println("Incorrect: The answer is LOWER than " + guess);
  30. }
  31. if(guess<ans)
  32. {
  33. System.out.println("Incorrect: The answer is HIGHER than " + guess);
  34. }
  35.  
  36. }
  37.  
  38. }while (x==1);
  39. String s = JOptionPane.showInputDialog("Would you like to play again? (Yes/No)");
  40. System.out.println(s);
  41. if (s == "no")
  42. {
  43. i++;
  44.  
  45. }
  46.  
  47.  
  48. }while (i==1);
  49.  
  50. }
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement