【问题标题】:Can't get out of my while loop无法摆脱我的 while 循环
【发布时间】:2013-06-28 14:43:44
【问题描述】:

我想知道是否有人可以告诉我为什么我的 while 循环表现得很有趣。当我输入一个 3 个单词的短语时,它应该创建一个首字母缩略词。由于我的java理解之外的原因,while循环执行但即使客户端输入正确答案也会继续执行。知道为什么吗?我已经阅读了数以百万计的帖子并观看了有关它的 youtube 视频,但没有任何线索。

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

【问题讨论】:

    标签: java string while-loop string-length word-count


    【解决方案1】:

    如果用户输入 4 个或更多单词,您的循环将失败。将wordCount != 3 更改为wordCount < 3 或准备处理较长的短语,例如北大西洋公约组织

    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 中的工作分解为更小的方法。现在,您可以测试这些方法了。

    【讨论】:

    • 一旦执行 do-while 循环,变量 'word' 和 'wordCount' 也永远不会被检查。如果第一个输入不是三个单词,则它是一个无限循环:|
    • 我相信我已经指出了这一点,说你也永远不会重新计算你的单词数组。然而,我并没有说他也需要重新计算 wordCount。我用伪代码做了我的版本,所以海报可以做点什么。
    【解决方案2】:

    您的循环在输入三个单词的短语后执行,因为正如 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 循环,您的变量wordwordCount 就不会更新。如果用户首先输入三个单词的短语,他们就可以了。如果他们输入任何其他长度的短语,程序将无限循环。由于程序仅在do-while 循环内执行逻辑,并且不包括将新单词保存到变量word 或计算它们的长度并将其存储在wordCount 中,因此无法退出循环。

    作为快速修复,我添加了这两行来解决这些问题:

    word = phrase.split("\\s+");
    wordCount = word.length;
    

    您也可以通过检查完全消除 wordCount 变量,但老实说,我不确定这样做与使用变量相比是否有价值。 phrase.split("\\s+").lengthwhile 循环中。

    【讨论】:

    • 看起来更好更短的选择,会试一试,
    【解决方案3】:

    您的主要问题是您的循环永远不会终止; wordCount 未在您的 do...while 块内更新。

    您的下一个问题是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+");
    }
    

    【讨论】:

    • 您应该将 (word.length
    • 考虑到评论暗示你要在少于三个字上出错,我认为word.length &lt; 3是合适的。
    • 谢谢诚!我与这个斗争了一段时间。
    【解决方案4】:

    解决了

    使用此代码 -

    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 + ".");
    
       } 
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 1970-01-01
      • 2016-09-25
      • 2014-03-28
      • 2022-12-02
      • 2013-06-20
      • 2022-12-18
      • 1970-01-01
      相关资源
      最近更新 更多