오류가 발생하도록 문자열을 입력으로 사용하여이 코드를 실행하면 오류 메시지가 무한 루프가됩니다. 나는 break를 삽입하려고 시도했다. 루프를 멈추고 프로그램을 멈추게하는 에러 메시지 이후에. 오류가 발생한 후 입력 요청으로 다시 루프를 보내고 싶습니다.오류가 발생하면 무한 루프가 발생합니다.
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();
}
}
'while (error)'을 사용 하시겠습니까? –
예, 맞습니다. – rocketboy