liangm20

Random Number

Nov 1st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. //this is Michelle's random number program
  2. import java.util.*;
  3. public class randomNumber
  4. {
  5. public static void main (String [] args)
  6. {
  7. Random rand = new Random(); //generating random number
  8. int x = rand.nextInt(10)+1;
  9. Scanner prompt = new Scanner(System.in); //scanner prompting user to guess a number
  10. System.out.print("Guess a number from 1-10. ");
  11. int y = prompt.nextInt();
  12. if (y>10 || y<1) //error message if guess is not between 1-10
  13. {
  14. System.out.print("Error! Not 1-10.");
  15. }
  16. else
  17. {
  18. if (y==x) //congratulations if guess=random number
  19. {
  20. System.out.print("Congratulations! You guessed the number!");
  21. }
  22. else
  23. {
  24. System.out.print("Sorry, the number was " + x + ".");
  25. if(y>x+3 || y<x-3) //if guess is 3 away from random number
  26. {
  27. System.out.print("You missed by a mile!");
  28. }
  29. else if (y!=x && y>10 && y<1) //if guess does not equal random number
  30. {
  31. System.out.print("You were close!");
  32. }
  33. System.out.print("Better luck next time :(");
  34. }
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment