如何使用 if 语句检查用户输入是否为特定变量类型?
How to use an if statement to check if user input is a certain variable type?
我正在创建一个数学程序,用户可以在其中输入答案。我想显示一条消息 "All solutions must be entered as decimal numbers"。我将如何确保用户输入双精度,如果不显示该消息。到目前为止我已经试过了:
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);
}
}
所以基本上我现在将其设置为显示答案是对还是错,但是如果用户输入非双精度,我怎么能制作另一个部分呢?感谢您的帮助:)
假设input
是一个Scanner
,那么你可以调用Scanner.hasNextDouble()
which returns true
if the next token in this scanner's input can be interpreted as a double value using the nextDouble()
方法。像,
if (input.hasNextDouble()) {
double userNum = input.nextDouble();
if (userNum == additionAnswer) {
System.out.println("That's correct!");
} else {
System.out.println("The correct solution is: " + additionAnswer);
}
} else {
System.out.println("All solutions must be entered as decimal numbers");
}
nextDouble
保证它只会扫描 看起来 像来自标准输入的双精度标记。既然你想捕获无效输入,你将不得不做更多的工作。
下面是可重入的;它将允许用户继续添加输入,直到引入有效输入。我将用户的提示留作 reader.
的练习
boolean valid = false;
do {
try {
double userNum = Double.parseDouble(input.nextLine());
// other code to use `userNum` here
valid = true;
} catch(NumberFormatException e) {
System.out.println("Not a valid double - please try again.");
}
} while(!valid);
您可以使用循环让用户重新输入数字,以防他输入错误。
System.out.println("Enter a double number:");
while (!input.hasNextDouble())
{
System.out.println("Invalid input\n Enter a double number:");
input.next();
}
double userInput = input.nextDouble();
System.out.println("You entered: " + userInput);
我正在创建一个数学程序,用户可以在其中输入答案。我想显示一条消息 "All solutions must be entered as decimal numbers"。我将如何确保用户输入双精度,如果不显示该消息。到目前为止我已经试过了:
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);
}
}
所以基本上我现在将其设置为显示答案是对还是错,但是如果用户输入非双精度,我怎么能制作另一个部分呢?感谢您的帮助:)
假设input
是一个Scanner
,那么你可以调用Scanner.hasNextDouble()
which returns true
if the next token in this scanner's input can be interpreted as a double value using the nextDouble()
方法。像,
if (input.hasNextDouble()) {
double userNum = input.nextDouble();
if (userNum == additionAnswer) {
System.out.println("That's correct!");
} else {
System.out.println("The correct solution is: " + additionAnswer);
}
} else {
System.out.println("All solutions must be entered as decimal numbers");
}
nextDouble
保证它只会扫描 看起来 像来自标准输入的双精度标记。既然你想捕获无效输入,你将不得不做更多的工作。
下面是可重入的;它将允许用户继续添加输入,直到引入有效输入。我将用户的提示留作 reader.
的练习boolean valid = false;
do {
try {
double userNum = Double.parseDouble(input.nextLine());
// other code to use `userNum` here
valid = true;
} catch(NumberFormatException e) {
System.out.println("Not a valid double - please try again.");
}
} while(!valid);
您可以使用循环让用户重新输入数字,以防他输入错误。
System.out.println("Enter a double number:");
while (!input.hasNextDouble())
{
System.out.println("Invalid input\n Enter a double number:");
input.next();
}
double userInput = input.nextDouble();
System.out.println("You entered: " + userInput);