1. import java.util.Scanner;
  2. 
  3. /**
  4. * HotOrCold.java
  5. * A guessing game.
  6. * @author Jose M Vidal <jmvidal@gmail.com>
  7. * Created on Jan 19, 2012
  8. *
  9. */
  10. 
  11. public class HotOrCold {
  12. 
  13. /**
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. final int maxNum = 1000; //the number of digits in the secret. I can change this later if I want to make it harder
  18. Scanner keyboard = new Scanner(System.in);
  19. System.out.println("I am thinking of a number between 0 and " + maxNum + ". Try to guess it.");
  20. int secret = (int)(Math.random() * maxNum);
  21. boolean done = false;
  22. while (! done) {
  23. System.out.print("Your guess:");
  24. int guess = keyboard.nextInt();
  25. if (guess >= 0 && guess <= maxNum ){
  26. int difference = Math.abs(guess - secret);
  27. if (difference == 0){
  28. System.out.println("You got it! Congratulations!");
  29. done = true; //the program is now done. exit the loop.
  30. }
  31. else if (difference < 5)
  32. System.out.println("You are Burning");
  33. else if (difference < 10)
  34. System.out.println("You are Hot");
  35. else if (difference < 100)
  36. System.out.println("Warm");
  37. else if (difference < 300)
  38. System.out.println("You are Cold");
  39. else
  40. System.out.println("You are Freezing");
  41. }
  42. else {
  43. System.out.println("Remember, I am thinking of a number between 0 and " + maxNum);
  44. }
  45. };
  46. System.out.println("Game Over");
  47. }
  48. 
  49. }