Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.4 kB
5
Indexable
Never
import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int randNumber = getRandomInt();
               System.out.println(randNumber);
               String userInputString = getUserNumber();

        while (!isNumeric(userInputString)){
            System.out.println( userInputString + " is not a number");
            userInputString = getUserNumber();
        }

       int userInputInt = Integer.parseInt(userInputString);
        
        while (randNumber != userInputInt){
            if(randNumber < userInputInt) {
                System.out.println("too much");
            } else if(randNumber > userInputInt ){
                System.out.println("too low");
            }else{
                System.out.println("you got it!");
            }
        }
    }
    public static int getRandomInt() {
        return (int) ((Math.random() * (100 - 1)) + 1);
    }
    public static String getUserNumber() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Guess the number");
        return scanner.nextLine();
    }

    public static boolean isNumeric(String strNum) {
        if (strNum == null) {
            return false;
        }
        try {
            int i = Integer.parseInt(strNum);
        } catch (NumberFormatException nfe) {

            return false;
        }
        return true;
    }
}