当此代码以字符串作为输入运行时,如果发生错误,则会导致错误消息的无限循环。我试过插入中断;在确实停止循环但也停止程序的错误消息之后。我希望它在发生错误后循环回输入请求。
import java.util.Scanner;
public class CubeUser
{
public static void main(String argv[])
{
Scanner in = new Scanner(System.in);
boolean error = true;
System.out.print("Please input the length of the cube:");
while(error == true)
{
if (in.hasNextDouble())
{
double length = in.nextDouble();
Cube cube1 = new Cube(length);
System.out.println("The surface area of cube1 is " + cube1.calculateSurfArea() );
System.out.println("The volume of cube1 is " + cube1.calculateVolume() );
error = false;
}
else
{
System.out.println("Please enter a numerical value for the cube's length.");
}
}
in.close();
}
}