Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. /*
  2. * Shane Lawler
  3. * 9/6/19
  4. *
  5. * This code creates a random number between one and ten, and gives the user three guesses to get the random number generated. The user then at the end of the game has an option to play again.
  6. *
  7. */
  8.  
  9.  
  10. import java.util.Scanner;
  11. public class GuessingGame
  12. {
  13.  
  14. public static void main(String [] args){
  15. int guesses = 3;
  16.  
  17. int answer = 0;
  18. Scanner sc = new Scanner(System.in);
  19.  
  20. int play = 1;
  21.  
  22. while (play == 1){
  23. answer = (int)(Math.random()*((10-1)+ 1))+ 1;
  24.  
  25.  
  26. while (guesses > 0){
  27. System.out.println("Enter a number between 1 and 10.");
  28. int guess = sc.nextInt();
  29.  
  30. if(answer == guess) {
  31. System.out.println("Winner!");
  32. answer = (int)(Math.random()*((10-1)+ 1))+ 1;
  33. System.out.println("Would you like to play again? (1 for Yes, 2 for No)");
  34. play = sc.nextInt();
  35. }
  36. if(answer != guess && guess <= 10 && guess >= 1){
  37. System.out.println("Sorry, Wrong!");
  38. guesses--;
  39. }
  40. if(answer != guess && (guess > 10 || guess < 1)){
  41. System.out.println("Pick a number between 1 and 10!!");
  42. }
  43. }
  44. if(guesses == 0){
  45. System.out.println("You Lose!! The number is " + answer + " Would you like to play again? (1 for Yes, 2 for No)");
  46. play = sc.nextInt();
  47. answer = (int)(Math.random()*((10-1)+ 1))+ 1;
  48. guesses = 3;
  49.  
  50. }
  51.  
  52. }
  53.  
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement