我怎样才能让这个 do while 循环停止而不“退出”导致打印输出?

问题描述

我曾尝试使用此代码,但每次它运行并输入退出时,它都会打印出结果,好像它应该是一个单词而不是结束循环的触发器,然后循环结束。我尝试只放入一个 if then 语句,如果单词退出,它将结束整个程序,但我无法摆脱具有单词不等于“退出”条件的 do while 循环。这是编码。

do
        {
            System.out.println("Enter a word and I will see if,when the first letter is made the last" + '\n' + "and this word is spelled backwards,is the original word. Type quit to end: ");
            word = keyboard.next().toLowerCase();
            
            String newWord = word.substring(1) + word.charat(0);
            String reverse = "";
        
            for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
            {
                reverse = reverse + newWord.charat(wordLength);
            }
        
            if (word.equals(reverse)) {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
            } else {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
            }
        }while (!word.equals("quit"));

一如既往,我感谢任何帮助!

解决方法

最简单的解决方案是将您的 else 语句替换为 if else 检查单词是否为 quit,因此 if-else 部分如下所示:

if (word.equals(reverse)) {
    System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
} else if(!word.equals("quit")) {
    System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
}
,

do while 循环仅在到达循环末尾时检查条件。因此,在您的代码中发生的情况是,当 word 设置为“退出”时,它下面的代码仍会运行,直到到达循环末尾。

要实现您想做的事,请尝试此操作,

do
        {
            System.out.println("Enter a word and I will see if,when the first letter is made the last" + '\n' + "and this word is spelled backwards,is the original word. Type quit to end: ");
            word = keyboard.next().toLowerCase();

            if(word.equals("quit")){ break; }

            String newWord = word.substring(1) + word.charAt(0);
            String reverse = "";
        
            for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
            {
                reverse = reverse + newWord.charAt(wordLength);
            }
        
            if (word.equals(reverse)) {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
            } else {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
            }
        }while (true);

这将导致 for 循环在输入退出时输出文本之前停止。

,

您可以通过简单的 while loop 更改所有这些。像这样:

   while(true) {
        System.out.println("Enter a word and I will see if,is the original word. Type quit to end: ");
        word = keyboard.nextLine().toLowerCase();
        if (word.contentEquals("quit")) break;
        
        String newWord = word.substring(1) + word.charAt(0);
        String reverse = "";
    
        for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
        {
            reverse = reverse + newWord.charAt(wordLength);
        }
    
        if (word.equals(reverse)) {
            System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!\n");
        } else {
            System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No\n");
        }
    }

这将一直运行,直到用户输入“退出”。

if (word.contentEquals("quit")) break;

这将打破循环,程序将停止。