我是编程新手很抱歉,如果这是一个愚蠢的问题,但我无法在任何地方找到答案。
我有一个扫描仪,询问数学问题的答案(扫描仪需要双倍作为输入)。当然,大多数人会输入一个数字作为答案,但如果他们输入其他东西(不能转换成双重),我应该给他们一条消息,说是以十进制形式。唯一的问题是我很难搞清楚告诉电脑是否(回答!=双)然后打印出“等等,等等,等等”。目前,如果您输入一个字母或其他内容,它会给出一个我不想要的计算机生成的红色错误消息。有没有办法让它打印出我想要的信息?
这是我的程序的一小部分,其中包含有问题的扫描程序:
if (letter.equalsIgnoreCase("A")) {
System.out.print("What is the solution to the problem: " + correctRange + " + " + correctRange2 + " = ");
// make the user answers a double variable
double answer;
answer = scnr.nextDouble();
double solution;
solution = correctRange + correctRange2;
// make a variable that is determined by if the answer is close
// enough
boolean close = ((solution + Config.CLOSE_ENOUGH) >= answer
&& answer >= (solution - Config.CLOSE_ENOUGH));
// give different responses based on if the user's answer was
// close
if (close) {
System.out.println("That is correct!");
} else {
System.out.println("The correct solution is " + solution + ".");
}
答案 0 :(得分:1)
if (scanner.hasNextDouble()) {
double d = scanner.nextDouble();
// do something with d
} else {
scanner.nextLine(); // discard the line
System.err.println("Please enter a number");
}
答案 1 :(得分:0)
你可以做
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
//my code goes here
}
或甚至更好
try{
result = scanner.nextDouble();
}catch (InputMismatchException ex){
System.out.println("blah blah");
}