我怎样才能在循环时停止此操作而不会"quit"导致打印输出?



我已经尝试过使用这段代码,但是每次它运行并且我键入quit时,它都会打印出结果,好像它应该是一个单词而不是一个结束循环的触发器,然后循环结束。我试着放入一个if then语句,如果单词是quit,它将结束整个程序,但是我无法摆脱do while循环,它的条件是word不等于&;quit&;。代码如下:

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"));
一如既往,我感谢任何帮助!

do while循环只在到达循环结束时检查条件。因此,代码中发生的情况是,当word被设置为"quit"下面的代码会一直运行,直到循环结束。

要达到你想要做的,试试这个,

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);

当输入quit时,这会导致for循环在输出文本之前停止。

您可以使用简单的while loop更改所有这些。这样的:

while(true) {
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.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? Non");
}
}

这将一直运行,直到用户输入"quit"。

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

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

最简单的解决方案是将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");
}

最新更新