-1

私の 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
4

4 に答える 4

0

解決済み

このコードを使用してください -

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

   } 
}
于 2013-06-28T06:43:44.467 に答える