Java Can';I don’我不能离开我的while循环
我想知道是否有人能告诉我为什么我的while循环很有趣。当我输入一个3个单词的短语时,它应该创建一个首字母缩略词。由于java无法理解的原因,while循环会执行,但即使在客户机输入正确答案时也会继续执行。知道为什么吗?我读过上百万篇文章,也看过youtube视频,但没有任何线索Java Can';I don’我不能离开我的while循环,java,string,while-loop,string-length,word-count,Java,String,While Loop,String Length,Word Count,我想知道是否有人能告诉我为什么我的while循环很有趣。当我输入一个3个单词的短语时,它应该创建一个首字母缩略词。由于java无法理解的原因,while循环会执行,但即使在客户机输入正确答案时也会继续执行。知道为什么吗?我读过上百万篇文章,也看过youtube视频,但没有任何线索 import javax.swing.*; import java.util.*; public class ThreeLetterAcronym { public static void main(Stri
import javax.swing.*;
import java.util.*;
public class ThreeLetterAcronym
{
public static void main(String args[])
{
//variables
String phrase;
int wordCount;
String acronym;
//input
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase.");
String [] word = phrase.split("\\s+");
wordCount = word.length;
//loop for error when less than 3 words
do
{
JOptionPane.showMessageDialog(null, "Error, you need to input a 3 word phrase to create a 3 letter acronym." + '\n'
+ "Please try again.");
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase");
}
while(wordCount !=3);
//create acronym
acronym = Character.toString(word[0].charAt(0)).toUpperCase() +
Character.toString(word[1].charAt(0)).toUpperCase() +
Character.toString(word[2].charAt(0)).toUpperCase();
//output
JOptionPane.showMessageDialog(null, "The phrase you inputed is " + "."
+ phrase + "\nYour 3 letter acronym is " + acronym + ".");
}
}
enter code here
如果用户键入4个或更多单词,循环将失败。更改
字数!=3
到字数<3
或准备好处理较长的短语,如北大西洋公约组织(North Atlantic Treaty Organization)
do while
循环始终至少执行一次。在这里,您正在检查错误,这实际上是合理的,但您也从未重新计算word
数组,因此错误情况仍然存在。你需要像这样的逻辑:
String[] words; // words, not word. There is more than one.
while (true) {
// 1. Fill in the word array from the user's input.
// 2. Check the input for errors.
// a. If there are none, break out of the loop with a break statement.
// b. Otherwise, show the error message.
}
// Create the acronym. Try to use a loop for this too, as in:
StringBuilder acronym = new StringBuilder();
for (String word: words) {
// Append to acronym.
}
// report acronym.
最后,尽量不要在
main
中完成所有工作。创建一个ThreeLetterAcronym
对象,给它实例变量和方法,并告诉它去做这项工作。将main
中的工作分解为较小的方法。现在,您可以测试这些方法。您的循环在输入三个单词短语后执行,因为正如Eric提到的,'do-while'
循环将始终至少执行一次。解决该问题的最佳简单解决方案是使用while
循环,如下所示:
while(wordCount != 3)
{
JOptionPane.showMessageDialog(null, "Error, you need to input a 3 word phrase to create a 3 letter acronym." + '\n'
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase");enter code here
}
do while
循环执行一个操作,然后检查是否应该再次执行该操作。while
循环检查它是否应该首先执行操作,这是您想要执行的操作
同时,还有一些其他重要问题值得解决 程序进入
do while
循环后,变量word
和wordCount
不会被更新。如果用户首先输入一个三个单词的短语,他们就可以了。如果他们输入任何其他长度的短语,程序将无限循环。由于程序仅在do while
循环中执行逻辑,而这不包括将新词保存到变量word
或计算其长度并在wordCount
中进行数据存储,因此无法退出循环
作为一个快速解决方案,我添加了以下两行来解决这些问题:
word = phrase.split("\\s+");
wordCount = word.length;
您也可以通过检查来完全消除wordCount变量,但老实说,我不确定这样做与使用该变量相比是否有价值<代码>短语.split(\\s+)。长度在
while
循环中。您的主要问题是循环永远不会终止<代码>字数未在do…中更新,而块
下一个问题是,do…while
循环总是至少运行一次
要解决此问题,您可以将word
的另一个实例化移动到循环体中,删除不必要的wordCount
变量(因为它是数组的一个属性,我们只在一个位置需要它),并将do…while
更改为while
//input
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase.");
String[] word = phrase.split("\\s+");
//loop for error when less than 3 words
while(word.length < 3) {
JOptionPane.showMessageDialog(null, "Error, you need to input a 3 word phrase to create a 3 letter acronym." + '\n'
+ "Please try again.");
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase");
word = phrase.split("\\s+");
}
//输入
phrase=JOptionPane.showInputDialog(null,“请输入三个单词的短语”);
字符串[]单词=短语。拆分(\\s+);
//少于3个字时循环查找错误
while(字长<3){
JOptionPane.showMessageDialog(null,“错误,您需要输入一个3个单词的短语来创建一个3个字母的首字母缩写。”+'\n”
+“请再试一次。”);
phrase=JOptionPane.showInputDialog(null,“请输入三个单词的短语”);
word=短语。拆分(\\s+);
}
已解决
使用此代码-
import javax.swing.*;
public class ThreeLetterAcronym
{
public static void main(String args[])
{
//variables
String phrase;
int wordCount;
String acronym;
//input
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase.");
String [] word = phrase.split(" ");
wordCount = word.length;
//loop for error when other than 3 words
while(wordCount!=3)
{
JOptionPane.showMessageDialog(null, "Error, you need to input a 3 word phrase to create a 3 letter acronym." + '\n'
+ "Please try again.");
phrase = JOptionPane.showInputDialog(null, "Please enter a three word phrase");
}
//create acronym
acronym = Character.toString(word[0].charAt(0)).toUpperCase() +
Character.toString(word[1].charAt(0)).toUpperCase() +
Character.toString(word[2].charAt(0)).toUpperCase();
//output
JOptionPane.showMessageDialog(null, "The phrase you inputed is " + "."
+ phrase + "\nYour 3 letter acronym is " + acronym + ".");
}
}
执行do while循环后,变量“word”和“wordCount”也不会被检查。如果第一个输入不是三个单词,那么它是一个无限循环:|我想我已经指出了这一点,表示您也从未重新计算您的单词数组。然而,我没有说他也需要重新计算字数。我用伪代码编写了我的版本,这样海报就可以做些事情了。你应该将(word.length<3)改为(word.length!=3),因为这将捕获三个单词以下和以上的长度,而不是刚刚低于三个单词。考虑到注释意味着你想在三个单词以下出错,我认为word.length<3
是合适的。谢谢Makoto!我为这一款苦苦挣扎了一段时间。看起来是一款更好、更短的选择,我会尝试一下,