即使while中的布尔值为false,循环仍会继续进行。我不知道怎么了。如果没有缓存他们的答案,则应该将布尔循环变量设置为false,从而使您退出循环,但由于某种原因,程序没有这样做。
public static double getvalue(String unit){
double answer = 0;
boolean loop = true;
do{
try{
System.out.println("enter the "+unit);
answer = sc.nextDouble();
loop = false;
}catch(Exception e){
System.out.println("has to be a double");
sc.nextLine();
}
}while(loop);
return answer;
}