samedi 30 janvier 2016

How to use an if statement to check if user input is a certain variable type?

I'm creating a math program where a user inputs an answer. I want to display a message that says "All solutions must be entered as decimal numbers". How would I make sure that the user inputs a double and if not display that message. I've tried this so far:

if(userLetter.equalsIgnoreCase("a")) {
    System.out.print("What is the solution to the problem:" + " " + ran1Shift + " " + "+" + " " + ran2Shift + " = ");
    double userNum = input.nextDouble();
        if(userNum == additionAnswer){
            System.out.println("That's correct!");
        }
        else{
            System.out.println("The correct solution is: " + additionAnswer);
        }
    }

So basically I have it set now to display whether the answer is exactly true or false but how could I make another part which catches if the user enters a non-double? Thank you for your help :)

Aucun commentaire:

Enregistrer un commentaire