Main.java 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. import java.util.*;
  5. public class Main {
  6. public static void main(String[] args){
  7. Random randomNumber = new Random();
  8. Scanner x = new Scanner(System.in);
  9. int computerNumber = randomNumber.nextInt(10);
  10. int numberOfTries = 0;
  11. int success = 0;
  12. int guess = 0;
  13. while (success ==0){
  14. System.out.println("Guess a number from 1 to 10");
  15. guess = x.nextInt();
  16. numberOfTries++;
  17. if (guess < 1 || guess >10){
  18. System.out.println("Invaled number. Try again");
  19. }
  20. else if (guess == computerNumber){
  21. System.out.println("Congratulations your right! Only took you " + numberOfTries + " tries.");
  22. }
  23. else if (guess < computerNumber){
  24. System.out.println("Too low! Try again.");
  25. }
  26. else if (guess > computerNumber){
  27. System.out.println("Too high! Try again.");
  28. }
  29. if (guess == computerNumber){
  30. break;
  31. }
  32. }
  33. }
  34. }