Java에 익숙하지 않습니다. 어리석은 질문 인 경우 죄송합니다. 루프 내부에서 두 개의 변수에 새로운 값을 할당하고 싶습니다.하지만 작동하지 않습니다. 컴파일을 시도 할 때 "로컬 변수 newString
이 초기화되지 않았을 수 있습니다"라는 오류 메시지가 나타납니다.while 루프 내에 변수를 설정할 수 없습니다.
public class Test {
public static String example() {
String first;
String second;
String newString;
int start = 1;
while (start<5) {
if (start<4) {
first = "hello";
second = "world";
newString = first + second;
}
start += 1;
}
return newString;
}
public static void main(String[] args) {
System.out.println(example());
}
}
''지역 변수 newString은 초기화되지 않았을 수 있습니다. '': 따라서 초기화하십시오 :'String newString = "";' –