我的 do/while 循环第二次把我的程序弄乱了

My do/while loop messes my program up the second time around

我遇到的问题是,一旦我输入 "non" 之类的词,我返回它是一个回文。然后我在询问 "Keep Going?" 时键入 "Yes"。它从 "Type Word:" 开始备份,我再次输入 "non"。然而这次我回来了,它不是回文。我认为这与我声明变量的方式或可能与 for 循环有关。不过我想不通。任何帮助表示赞赏。

import java.util.*;
public class palindromeTest
{
 public static void main (String [] args)
 {
  Scanner in = new Scanner (System.in);
  String word = "", backword = "", exit = "";
  int length;
  do{
     System.out.print ("Type a Word: ");
     word = in.nextLine();

     length = word.length();

     for (int i = length - 1; i >= 0; i--)
        backword = backword + word.charAt(i);
     if (word.equalsIgnoreCase(backword))
        System.out.println ("This IS a Palindrome.");
     else 
        System.out.println ("This is Not a Palindrome.");

     System.out.println ("Keep Going? /n Yes or No?");
     exit = in.nextLine();
  }while(exit.equalsIgnoreCase ("Yes"));
}//end main
}//end palindromeTest

backward 的声明移动到循环体中(因此它在每次迭代时重置),

String word = "", exit = "";
int length;
do {
    String backword = "";

但是,将 StringBuilder 用于 String 串联是一个更好的主意,但我会将优化留作 reader.

的练习。