"banana"와 같은 단어를 사용자가 사용하는 프로그램을 작성하는 중 "b"단어의 첫 글자가 맨 끝에 표시됩니다. 그런 다음 ananab은 동일한 단어의 철자가 맞는지 확인합니다. 며칠 동안이 작업을 해왔고 몇 가지 시도를했지만 여전히 for 루프 내부에서 하나의 문자열을 검사하는 방법에 대해 확신이 서지 않았습니다. 지금까지 내 프로그램입니다.자바의 루프 바깥 쪽 문자열과 루프 바깥 쪽 문자열 비교
public static void main(String[] args) {
System.out.println("Enter words that can be checked for backward spelling");
System.out.println("Please enter a word to check");
Scanner keyboard = new Scanner(System.in);
String words = keyboard.nextLine();
String firstLetter = String.valueOf(words.charAt(0));
String words2 = words.substring(1);
String otherwords = words2+firstLetter;
for (int i=otherwords.length()-1; i>=0; i--){
String newwords=String.valueOf(otherwords.charAt(i));
boolean match = newwords.equalsIgnoreCase(words);
if (match){
System.out.println("This word matches the criteria we are lookin for");}
}
}
}
가 좋아, 내가 문자열을 반대로 필요하기 때문에 그렇지 끝에 문자를 추가하면 내가 루프를 추가 감사합니다 ... 그것은 같은 단어가 아닐 것이다. 그것은 바나나 대신 ananab 될 루프없이 문자열을 되돌릴 수있는 다른 방법이 있나요, 이전에 문자열 버퍼를 사용하려고 다시 시도해야합니까? –
문자열을 되돌릴 필요가 없습니다. 우리는 결코 그것을 잃어 버리지 않았습니다. 사용자가 "banana"를 입력하면 String 입력은 "banana"가되고 String newString은 "ananab"이됩니다. if 문 다음에는 문자열 입력이 여전히 있습니다. – Solace