1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Write a description of class GuessGame here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.util.Random;
  8. import java.util.Scanner;
  9. public class GuessGame
  10. {
  11. // instance variables - replace the example below with your own
  12. private int x;
  13. /**
  14. * Constructor for objects of class GuessGame
  15. */
  16. public static void main(String[] args){
  17. System.out.println("Hello and welcome to my number guessing game.");
  18. System.out.println("Pick a number 1 through 10: ");
  19. Scanner input = new Scanner(System.in);
  20. int guess;
  21. int counter = 0;
  22. int prevGuess = -1;
  23. int rand = (int) (Math.random()*10)+1;
  24. boolean notCorrect = true;
  25. while(notCorrect){
  26. guess = input.nextInt();
  27. if(guess != prevGuess){
  28. counter++;
  29. }
  30. prevGuess = guess;
  31. if(guess > rand){
  32. System.out.println("Your guess is too high");
  33. } else if(guess < rand){
  34. System.out.println("Your guess is too low");
  35. }else {
  36. notCorrect = false;
  37. System.out.println("You got it! " + counter + " guesses");
  38. }
  39. }
  40. /*if (guess == number){
  41. System.out.println("You got it!");
  42. }
  43. else if (guess < number){
  44. System.out.println("Your guess is too low");
  45. }
  46. else if (guess > number){
  47. System.out.println("Your guess is too high");
  48. }*/
  49. }
  50. }