我怎样才能让这个 do while 循环停止而不 "quit" 导致打印输出?
How can I get this do while loop to stop without "quit" causing a printout?
我试过使用这段代码,但每次它运行并且我输入 quit 时,它都会打印出结果,就好像它应该是一个单词而不是结束循环的触发器,然后循环结束。我尝试只放入一个 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, 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? No\n");
}
}
这将 运行 直到用户输入“退出”。
if (word.contentEquals("quit")) break;
这将打破循环,程序将停止。
我试过使用这段代码,但每次它运行并且我输入 quit 时,它都会打印出结果,就好像它应该是一个单词而不是结束循环的触发器,然后循环结束。我尝试只放入一个 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, 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? No\n");
}
}
这将 运行 直到用户输入“退出”。
if (word.contentEquals("quit")) break;
这将打破循环,程序将停止。