[英]Printing multiple JOptionPane string input dialogs in one sentence
I want to read input from multiple JOptionPane input dialogs and print the input from each dialog in a JOptionPane Message Dialog in once sentence. 我想从多个JOptionPane输入对话框中读取输入,并在一个句子中将来自每个对话框的输入打印到JOptionPane消息对话框中。 For example: This, is, a, message.
例如:这是一条消息。
would output as: This is a message 将输出为:这是一条消息
Here is my code which I am trying to adapt, it currently calculates the total amount of characters in all of the inputs. 这是我尝试修改的代码,它当前计算所有输入中的字符总数。
// 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);
}
}
Just use a StringBuilder
to concatenate each new word. 只需使用
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.