Guest User

Untitled

a guest
Dec 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.util.*;
  3. public class HighLow {
  4. public static void main(String[] args) {
  5.  
  6. //variables
  7. int chances = 12;
  8. int selection;
  9. int comp = (int) Math.floor(Math.random() * (200) + 1);
  10.  
  11. boolean playAgain = true;
  12. while (playAgain) {
  13.  
  14. //input window for player
  15. JOptionPane.showMessageDialog(null, "This is a game of High-Low. This computer has chosen a number from 1 to 200 and you have to guess what that number is within 13 guesses.");
  16.  
  17. //player's input , the loops
  18. for (chances = 12; chances > 0; chances--){
  19.  
  20. String user = JOptionPane.showInputDialog(null, "What do you think the number is? Enter your guess.");
  21. int guess = Integer.parseInt(user);
  22.  
  23. if (guess < comp) {
  24. System.out.println("Your guess is too low, try again");
  25. System.out.println("You now have " + chances + " tries left");
  26. } else
  27. if (guess > comp) {
  28. System.out.println("Your guess is too high, try again");
  29. System.out.println("You now have " + chances + " tries left");
  30. } else
  31. if (guess == comp) {
  32. System.out.println("Congrats, your guess is correct! The number was " + comp + ".");
  33. break;
  34. }
  35.  
  36. if (chances == 0) {
  37. System.out.println("Your guess is incorrect.");
  38. System.out.println("Sorry, but you are out of chances!");
  39. break;
  40. }
  41.  
  42. }
  43.  
  44. //ask if the user wants to play again
  45. selection = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Otra vez!", JOptionPane.YES_NO_OPTION);
  46. playAgain = (selection == JOptionPane.YES_OPTION);
  47.  
  48. }
  49. }
  50. }
Add Comment
Please, Sign In to add comment