do-while 루프를 사용하는 코드로 작업하고 있었고 if 루프에 if else 문을 추가하려고했습니다. do-while 루프는 사용자가 입력 한 텍스트를 확인하고 'exit'라는 단어가 입력되면 완료됩니다. 이 코드를 컴파일 할 때while 문을 while 루프에 중첩 할 수 있습니까?
public static void main(String[] args) {
String endProgram = "exit";
String userInput;
java.util.Scanner input = new java.util.Scanner(System.in);
do {
userInput = input.nextLine();
if (!userInput.equalsIgnoreCase(endProgram)) {
System.out.printf("You entered the following: %s", userInput);
} else
} while (!userInput.equalsIgnoreCase(endProgram));
}
, 나는이 말을 명령 프롬프트에서 오류가 발생합니다 :
SentinelExample.java:20: error: illegal start of expression
} while (!userInput.equalsIgnoreCase(endProgram));
^
SentinelExample.java:22: error: while expected
}
^
SentinelExample.java:24: error: reached end of file while parsing
}
^
내가 루프의 경우 다른 문을 제거하면, 프로그램은 잘 컴파일합니다. 내 프로그램의 문법에 문제가 있습니까? 또는 while 루프에 if else 문을 넣을 수 없습니까?
왜 안 보이는지. –
* if 문을 while 루프에 중첩시킬 수 있습니까? 예 –
거기에 쓸모없는 else 키워드가 있습니다. –