Main.java 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. import java.util.Scanner;
  5. import java.util.Random;
  6. public class Main {
  7. public static void main(String[] args){
  8. Scanner s = new Scanner(System.in);
  9. Random rand = new Random();
  10. int magicNum = rand.nextInt(10) + 1; //Generate a random number between 1 and 10
  11. int currGuess = -1;
  12. int previousGuess = -2;
  13. int guessCount = 0;
  14. System.out.println("It's a guessing game! Guess the magic number! It's between 1 and 10!");
  15. System.out.print("Guess a number: ");
  16. while (magicNum != currGuess) {
  17. currGuess = s.nextInt();
  18. if (currGuess != previousGuess) {
  19. guessCount++;
  20. }
  21. if (currGuess == magicNum) {
  22. System.out.println("Congrats! You guessed the magic number in "
  23. + guessCount + " guesses!");
  24. }
  25. else if (currGuess < magicNum) {
  26. System.out.print("Too low! Guess a number: ");
  27. }
  28. else if (currGuess > magicNum) {
  29. System.out.print("Too high! Guess a number: ");
  30. }
  31. previousGuess = currGuess;
  32. }
  33. s.close();
  34. }
  35. }