-5
나는 프로그램을 가지고 있지만, 멈추라 고 말한 후에도 계속 실행되는 문제가있다. Boolean 변수가 false가 될 때 실행을 멈추라 고했지만 변수가 false가 된 후에도 프로그램은 계속 실행됩니다. 프로그램을 계속 실행하는 유일한 방법은 while 문에서 "= true"부분을 제거하는 것입니다. 그렇다면 프로그램이 정상적으로 작동하려면 명세서에서 "= true"부분을 제거해야하는 이유는 무엇입니까?예외 프로그램이 계속 실행되는 이유는 무엇입니까?
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int num;
boolean continueLoop=true;
do {
try {
System.out.println("Enter an even number");
num=input.nextInt();
if(num%2==0)
System.out.println("That is an even number");
else
throw new IllegalArgumentException();
continueLoop=false;
}
catch(IllegalArgumentException ae) {
System.err.println(ae+"\nThat is a odd number");
}
} while(continueLoop=true);
input.close();
}
}
'continueLoop = true'가 할당이고, 항상 그 행에서 variabe를 true로 설정합니다. 기본적인 자바 튜토리얼을 살펴보면 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html –
을 제거하고'= true '를 제거하면 해당 수정 사항을 알 수 있습니다. 'someBoolean == true'를 비교하는 것은 중복됩니다. 'someBoolean'만으로도 충분합니다. –
신중하게 이것을 검토하십시오 : while (continueLoop = true); –