简体   繁体   English

轻松修复while循环第二次无法正常执行?

[英]Easy fix for while loop not executing properly second time?

I'm fairly new at java and have a current assignment to take a given word, put the first word at the end, rebuild the word from reverse, and see if it's the same word as the original, such as: grammar, potato, uneven, dresser, banana etc. So far I have this: 我在Java领域还很陌生,现在有一个任务,可以接受一个给定的单词,将第一个单词放在最后,从相反的方向重建该单词,然后查看它是否与原始单词相同,例如:语法,土豆,不均匀,梳妆台,香蕉等。到目前为止,我有:

    Scanner input = new Scanner(System.in);
    String original, reverse = "";
    String exit = "quit";
    int index;

    System.out.println("Please enter a word (enter quit to exit the program): ");
    original = input.next();

    while (!original.equalsIgnoreCase(exit))
    {
        String endingChar = original.substring(0, 1);
        String addingPhrase = original.substring(1);
        reverse += endingChar;
        for (index = addingPhrase.length() - 1; index >= 0; --index)
        {
            char ch = addingPhrase.charAt(index);
            reverse += ch;
        }
        if (original.equals(reverse))
        {
            System.out.println("Success! The word you entered does have the gramatic property.");
        }
        else 
        {
            System.out.println("The word you entered does not have the gramatic property."
                    + " Please try again with another word (enter quit to exit the program): ");
        }
        original = input.next();
    }
    input.close();

When I run it and enter the word "banana," it properly recognizes that it is indeed the same backwards when the b is moved to the end, and does the same with the other words listed above, but when I enter a second word on the loop, it never recognizes it properly, and always responds with the print statement from the else block: 当我运行它并输入单词“ banana”时,它会正确识别出,当b移到末尾时,它确实向后移动,并且对上面列出的其他单词也是如此,但是当我在上输入第二个单词时循环,它永远无法正确识别它,并始终以else块的print语句作为响应:

Please enter a word (enter quit to exit the program): 
banana
Success! The word you entered does have the gramatic property.
banana
The word you entered does not have the gramatic property. Please try again 
with another word (enter quit to exit the program): 

I'm guessing it's something to do with either the way I made my for loop, or the way I asked for input at the end of the while loop, but like I said I'm fairly new and awful at debugging. 我猜测这与我制作for循环的方式或在while循环结束时要求输入的方式有关,但是就像我说的那样,我在调试方面相当陌生。 Any help would be much appreciated, thanks a lot in advance. 任何帮助将不胜感激,非常感谢。

You are changing string reverse in every iteration, but you are not clearing it. 您在每次迭代中都将字符串reverse更改,但是您没有清除它。 So before the end of the loop or at the beginning clear the string for example like so: reverse = "" , and then it should be fine. 因此,在循环结束之前或开始时,请清除字符串,例如: reverse = "" ,然后应该可以。

Just add reverse = ""; 只需添加反向=“”; in the end of the while loop in order to set the variable reverse to its original state, ie empty string 在while循环的末尾,以便将变量设置为其原始状态,即空字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM