Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. /**
  2. *This code generates a random number and gives the user 3 chances to guess the number
  3. *
  4. * @Molly Limaye
  5. * @09/05/19
  6. */
  7.  
  8. import java.util.*;
  9.  
  10. public class GuessingGame {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Scanner sc = new Scanner(System.in);
  15.  
  16. //play again loop
  17. int play = 1;
  18. while (play == 1){
  19. //generate random number
  20. int num = (int)(Math.random() * ((10-1)+1))+1;
  21.  
  22. //user guessing
  23. System.out.println("Guess a number between 1 and 10.");
  24. for(int i=0; i<3; i++) {
  25. int guess = sc.nextInt();
  26.  
  27. //make sure guess is between 1 and 10
  28. if((guess>10)||(guess<1)){
  29. System.out.println("Please guess a number between 1 and 10.");
  30. i=0;
  31. guess = sc.nextInt();
  32. }
  33.  
  34. //results of guess
  35. if (guess==num){
  36. System.out.println("Winner!");
  37. i=4;
  38. }
  39. else{
  40. System.out.println("Sorry, wrong.");
  41. if(i<2){
  42. System.out.println("Try again");
  43. }
  44. }
  45. }
  46.  
  47. System.out.println("The answer was " + num + ".");
  48. //play again feature
  49. System.out.println("Would you like to play again? (1=yes/2=no)");
  50. int answer = sc.nextInt();
  51. if (answer==1){
  52. play = 1;
  53. }
  54. else{
  55. play = 0;
  56. }
  57.  
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement