我将InputMismatchException
放在一个我不应该得到的地方。我要求用户输入cm
或kg
,具体取决于他们要转换为哪一个。我使用sc.next
来获取他们的输入,但是即使它是字符串,它也会引发异常。
我尝试将其他sc.""
弄乱,以查看是否使用了错误的userChoice = ""
。我还尝试将字符串cm
更改为 String userChoice = " ";
String userCont = "yes";
System.out.println("\t\t\tWelcome to the Standard to Metric Converter.\n");
while (userCont.equalsIgnoreCase("yes")){
System.out.println("Type \"kg\" to convert to kilograms.");
System.out.println("Type \"cm\" to convert to centimeters.");
sc.next(userChoice);
if (userChoice.equalsIgnoreCase("cm")){
System.out.println("You have chosen to convert feet and inches to "
+ "centimeters.");
System.out.print("Enter the amount of feet: \n");
sc.nextInt(conv.feet);
System.out.print("Enter the amount of inches: \n");
sc.nextInt(conv.inch);
}
}
只是为了看看会发生什么(充满很多错误的巨大混乱)。
^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*(\+[a-zA-Z0-9-]+)?@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$
答案 0 :(得分:2)
在您的示例中,您实际上并未为userChoice
分配新值(我想您尝试这样做)。您使用userChoice
作为模式来查找新值,例如,该值是空的。
应该是:
userChoice = sc.next();
其余的代码也一样。