Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Problem1
  3. {
  4. public static void main (String [] args)
  5. {
  6. Scanner user_input = new Scanner (System.in);
  7.  
  8. int rand;
  9. int guessNum;
  10.  
  11. rand = (int)(Math.random() * 101);
  12. System.out.print ("I have a number between 1 and 100");
  13. System.out.println ("\n");
  14. System.out.print ("What is your guess: ");
  15. guessNum = user_input.nextInt ();
  16.  
  17. int count = 1;
  18.  
  19. do
  20. {
  21. if (guessNum > rand)
  22. {
  23. System.out.print ("Go lower!");
  24. System.out.print ("\nWhat is your guess: ");
  25. guessNum = user_input.nextInt ();
  26.  
  27. }
  28.  
  29. if (guessNum < rand)
  30. {
  31. System.out.print ("Go higher!");
  32. System.out.print ("\nWhat is your guess: ");
  33. guessNum = user_input.nextInt ();
  34. }
  35.  
  36. if (guessNum == rand)
  37. {
  38. System.out.print ("You got it!");
  39. }
  40.  
  41.  
  42. count ++;
  43.  
  44. } while (guessNum != rand);
  45.  
  46. System.out.println ("\n");
  47. System.out.print ("It took " + count + " guesses");
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement