public class Calculator
{
public static void main(String[] args)
{
boolean isValid = false;
Scanner myScanner = new Scanner(System.in);
String customerType = null;
System.out.print("Customer Type? (C/R) ");
customerType = myScanner.next();
while (isValid == false)
{
System.out.print("Enter Subtotal: ");
if (myScanner.hasNextDouble())
{
double sobTotal = myScanner.nextDouble();
isValid = true;
}
else
{
System.out
.println("Hay! Entry error please enter a valid number");
}
myScanner.nextLine();
}
}
}
嗨,我是 java 新手,我像往常一样在 Scanner 类中尝试了一些东西。
有没有办法查看扫描仪的输入?因为你会看到我上面的代码有问题。这是我输入错误数据后控制台窗口的输出。我输入的是 KKK 而不是数字,所以有人可以解释一下为什么我两次收到此错误消息吗?
"this is the console"
Customer Type? (C/R) R
Enter Subtotal: KKK
Hay! Entry error please enter a valid number
Enter Subtotal: Hay! Entry error please enter a valid number
Enter Subtotal: