Advertisement
Guest User

guessing game

a guest
Nov 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class MyClass {
  5. public static void main(String args[]) {
  6.  
  7. Scanner in = new Scanner(System.in);
  8. Random r = new Random();
  9.  
  10. int userInput = 0;
  11. int numTries = 0;
  12. int randomNum = r.nextInt(10) + 1;
  13. boolean win = false;
  14.  
  15. while (!win) {
  16. userInput = in.nextInt();
  17. numTries++;
  18.  
  19. if (randomNum == userInput) {
  20. win = true;
  21. }else {
  22. System.out.println("Sorry, please enter another number:");
  23. }
  24. }
  25.  
  26. System.out.println("Congratulations! you have guessed the number correctly!");
  27. System.out.println("You took " + numTries + " number of tries");
  28. System.out.println(randomNum);
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement