在java中使用while循环编写回文字符串的最简单方法是什么

在java中使用while循环编写回文字符串的最简单方法是什么,java,while-loop,Java,While Loop,我到处找了,但就是找不到任何具体的东西。我已经为这个代码工作了一段时间,但它一直困扰着我 public static void main(String[] args) { System.out.println(palindrome("word")); } public static boolean palindrome(String myPString) { Scanner in = new Scanner(System.in); System.out.println

我到处找了,但就是找不到任何具体的东西。我已经为这个代码工作了一段时间,但它一直困扰着我

public static void main(String[] args) {
    System.out.println(palindrome("word"));
}

public static boolean palindrome(String myPString) {
    Scanner in = new Scanner(System.in);

    System.out.println("Enter a word:");
    String word = in.nextLine();
    String reverse = "";
    int startIndex = 0;
    int str = word.length() -1;

    while(str >= 0) {
        reverse = reverse + word.charAt(i);
    }
}

有很多方法可以通过
while
循环来实现这一点。
考虑到简单性,你可以想象,如果你面前的桌子上有一组塑料分隔字符,你怎么能做到这一点。
也许你会考虑得到第二个角色并将其移动到开始,然后得到第三个角色并移动到开始,依此类推,直到到达最后一个角色,对吗

0123    1023    2103    3210
WORD -> OWRD -> ROWD -> DROW
因此,您只需要两个代码:

init a variable i with 1 (the first moved character)
while the value of i is smaller than total string size do
    replace the string with 
       char at i plus
       substring from 0 to i plus
       substring from i+1 to end
    increment i
print the string
这一过程应当是:

o + w + rd
r + ow + d
d + row + 

drow

希望能有所帮助

这是我不久前写的一段代码,它使用了几乎相同的过程。希望有帮助

    String original;
    String reverse = "";

    System.out.print("Enter a string: ");
    original = input.nextLine();
    for(int x = original.length(); x > 0; x--)
    {
        reverse += original.charAt(x - 1);
    }

    System.out.println("The reversed string is " +reverse);

什么是
i
str
在哪里减量?看起来您正在按计划反转单词,而不是创建或检查回文。。。不清楚你期望的是什么输出为什么你要用扫描仪?你把一个字符串作为一个参数“我到处都找了,但我就是找不到任何具体的东西,我已经在这个代码上工作了一段时间,但它一直困扰着我。”-我认为你这样做是错误的。你不会通过寻找解决方案来学习编程。这就像在数学课本的后面查找答案一样。通过实际编程学习编程。我的建议是:停止谷歌搜索,停止向我们寻求帮助,继续努力。是的,不要试图复制代码——从头开始。首先写“伪代码”——用英语写几行解释你在每一步做什么(或者画一个流程图)。然后把这些句子翻译成你的目标语言。