입력 된 단어가 회문인지 아닌지를 테스트하려고합니다 (앞뒤 철자가 같음). 내가 볼 수있는 것부터는 작동하지만 Eclipse는 "로컬 변수 isPalindrome의 값은 사용되지 않습니다"라고 말하지만 사용됩니다. 문제는 단어가 회문이 아니더라도 그것이라고 말합니다.로컬 변수가 사용되지 않습니다.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
String phrase;
char[] phraseLetters;
int endChar;
boolean isPalindrome;
Scanner input = new Scanner(System.in);
System.out.println("Enter a word or phrase.");
phrase = input.nextLine();
input.close();
phrase = phrase.toLowerCase();
phrase = phrase.replaceAll(" ","");
phraseLetters = phrase.toCharArray();
endChar = phraseLetters.length - 1;
for (int i = 0; i < phraseLetters.length; i++) {
if (phraseLetters[i] != phraseLetters[endChar]) {
isPalindrome = false;
} else {
isPalindrome = true;
endChar -= 1;
}
}
if (isPalindrome = true) {
System.out.println("This word or phrase entered is a palindrome.");
} else {
System.out.println("This word or phrase is not a palindrome.");
}
}
}
편집 : 나는이에 "지역 변수 isPalindrome가 초기화되지 않았을 수 있습니다"이클립스는 말한다 두 경우 모두
if (isPalindrome == true)
및
if (isPalindrome)
을되는 경우 문을 시도 조건 일 경우.
최종 편집 : 이후에 이동하고,이 코드를 다시 한
그러나 나는 그냥 가서 사람이 여전히이 보이는 경우 내 원래의 코드를 수정했습니다.
for (int i = 0; (i < phraseLetters.length) && (isPalindrome); i++)
if (isPalindrome)
이 오류를 방지하기 위해 yoda 조건을 살펴보십시오. –