2016-10-10 20 views
0

複数のJOptionPane入力ダイアログから入力を読み込み、JOptionPaneメッセージダイアログの各ダイアログからの入力を一度に出力したいと考えています。例: これは、aです。メッセージです。複数のJOptionPane文字列入力ダイアログを1つの文で出力する

として出力になります。これは、ここでメッセージ

ですが、私は適応しようとしている私のコードですが、それは現在、すべての入力中の文字の合計金額を計算します。

// A Java Program by Gavin Coll 15306076 to count the total number of characters in words entered by a user // 
import javax.swing.JOptionPane; 

public class WordsLength  
{ 
public static void main(String[] args) 
{ 
    String words = JOptionPane.showInputDialog(null, "Enter your word: "); // Getting the first word 

    int length = words.length(); 

    int totallength = length; 
    int secondaryLength; 

    do 
    { 
     String newwords = JOptionPane.showInputDialog(null, "Enter another word: (Enter nothing to stop entering words) "); // Getting more words 
     secondaryLength = newwords.length(); // Getting the length of the new words 

     totallength += secondaryLength; // Adding the new length to the total length 

    } 

    while(secondaryLength != 0); 

    JOptionPane.showMessageDialog(null, "The total number of characters in those words is: " + totallength); 

    System.exit(0); 
} 
} 

答えて

0

新しい単語を連結するには、StringBuilderを使用してください。

import javax.swing.JOptionPane; 

    public class WordsLength { 
     public static void main(final String[] args) { 
      String words = JOptionPane.showInputDialog(null, "Enter your word: "); // Getting the first word 

      int length = words.length(); 

      int secondaryLength; 
      int totallength = length; 

      StringBuilder builder = new StringBuilder(); 
      builder.append(words); 

      do { 
       String newwords = JOptionPane.showInputDialog(null, 
         "Enter another word: (Enter nothing to stop entering words) "); // Getting more words 

       secondaryLength = newwords.length(); // Getting the length of the new words 

       totallength += secondaryLength; // Adding the new length to the total length 

       builder.append(' '); 
       builder.append(newwords); 

      } 

      while (secondaryLength != 0); 

      JOptionPane.showMessageDialog(null, "The total number of characters in those words is : " + totallength); 
      JOptionPane.showMessageDialog(null, "The full sentence is : " + builder); 

      System.exit(0); 
     } 
    } 
関連する問題