Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5. public class GuessNumberApp {
  6.  
  7. public static void main(String[] args) {
  8. final int LIMIT = 10;
  9.  
  10. System.out.println("Guess the number!");
  11. System.out.println("I'm thinking of a number from 1 to " + LIMIT);
  12. System.out.println("Try to guess it in 10 seconds");
  13.  
  14.  
  15.  
  16.  
  17.  
  18. // get a random number between 1 and the limit
  19. double d = Math.random() * LIMIT; // d is >= 0.0 and < limit
  20. int number = (int) d; // convert double to int
  21. number++; // int is >= 1 and <= limit
  22.  
  23. Scanner sc = new Scanner(System.in);
  24. int count = 0;
  25. int guess = 0;
  26. while (guess != number) {
  27.  
  28. System.out.println("Timer started!");
  29.  
  30. System.out.print("Your guess: ");
  31. guess = sc.nextInt();
  32.  
  33.  
  34.  
  35. if (guess < 1 || guess > LIMIT) {
  36. System.out.println("Invalid guess. Try again.");
  37. count--;
  38. } else if (guess < number) {
  39. System.out.println("Too low.");
  40. } else if (guess > number) {
  41. System.out.println("Too high.");
  42. }
  43.  
  44. count++;
  45. }
  46. System.out.println("You guessed it in " +
  47. count + " tries.\n");
  48. System.out.println("Bye!");
  49. }
  50. }
  51. if anyone is familiar with timer thread here is my code need to add a timer when game starts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement