在单个对话框中打印数组 (Java)
Printing an array in a single dialog box (Java)
这是我当前的代码。它可以工作,但会在多个对话框中输出数字。我不知道我应该如何在单个对话框中打印它们,用行分隔。
import javax.swing.JOptionPane;
public class SecondClass
{
public static void main (String args[])
{
int stringLength;
char num[];
String value = JOptionPane.showInputDialog (null, "Input numbers", JOptionPane.QUESTION_MESSAGE); //input
stringLength = value.length(); //getting string length and converting it to int
num = value.toCharArray(); //assigning each character to array num[]
for (int i = 0; i <= stringLength; i++) {
JOptionPane.showMessageDialog (null, "You entered " + num[i] , "Value", JOptionPane.INFORMATION_MESSAGE); //output box
}
}
}
为什么不使用 StringBuffer 将所有答案放在一个长字符串中(当然用换行符分隔),然后在完成后将其显示在对话框中?
更正此片段:
for (int i = 0; i <= stringLength; i++) {
JOptionPane.showMessageDialog (null, "You entered " +
num[i] , "Value",
JOptionPane.INFORMATION_MESSAGE); //output box
}
到
String out="";
for (int i = 0; i < stringLength; i++) {
out+= "You entered " + num[i] ;
}
JOptionPane.showMessageDialog (null, out, "Value\n", JOptionPane.INFORMATION_MESSAGE);
不确定为什么需要额外的 loop
,当您可以直接显示输出时:
public class SecondClass {
public static void main(String args[]) {
String value = JOptionPane.showInputDialog(null, "Input numbers",
JOptionPane.QUESTION_MESSAGE); // input
JOptionPane.showMessageDialog(null, "You entered " + value, "Value",
JOptionPane.INFORMATION_MESSAGE); // output box
}
}
循环版本:
public class SecondClass {
public static void main(String args[]) {
char num[];
String value = JOptionPane.showInputDialog(null, "Input numbers",
JOptionPane.QUESTION_MESSAGE); // input
num = value.toCharArray(); // assigning each character to array num[]
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
builder.append(num[i]);
}
JOptionPane.showMessageDialog(null, "You entered " + builder, "Value",
JOptionPane.INFORMATION_MESSAGE); // output box
}
}
这是我当前的代码。它可以工作,但会在多个对话框中输出数字。我不知道我应该如何在单个对话框中打印它们,用行分隔。
import javax.swing.JOptionPane;
public class SecondClass
{
public static void main (String args[])
{
int stringLength;
char num[];
String value = JOptionPane.showInputDialog (null, "Input numbers", JOptionPane.QUESTION_MESSAGE); //input
stringLength = value.length(); //getting string length and converting it to int
num = value.toCharArray(); //assigning each character to array num[]
for (int i = 0; i <= stringLength; i++) {
JOptionPane.showMessageDialog (null, "You entered " + num[i] , "Value", JOptionPane.INFORMATION_MESSAGE); //output box
}
}
}
为什么不使用 StringBuffer 将所有答案放在一个长字符串中(当然用换行符分隔),然后在完成后将其显示在对话框中?
更正此片段:
for (int i = 0; i <= stringLength; i++) {
JOptionPane.showMessageDialog (null, "You entered " +
num[i] , "Value",
JOptionPane.INFORMATION_MESSAGE); //output box
}
到
String out="";
for (int i = 0; i < stringLength; i++) {
out+= "You entered " + num[i] ;
}
JOptionPane.showMessageDialog (null, out, "Value\n", JOptionPane.INFORMATION_MESSAGE);
不确定为什么需要额外的 loop
,当您可以直接显示输出时:
public class SecondClass {
public static void main(String args[]) {
String value = JOptionPane.showInputDialog(null, "Input numbers",
JOptionPane.QUESTION_MESSAGE); // input
JOptionPane.showMessageDialog(null, "You entered " + value, "Value",
JOptionPane.INFORMATION_MESSAGE); // output box
}
}
循环版本:
public class SecondClass {
public static void main(String args[]) {
char num[];
String value = JOptionPane.showInputDialog(null, "Input numbers",
JOptionPane.QUESTION_MESSAGE); // input
num = value.toCharArray(); // assigning each character to array num[]
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
builder.append(num[i]);
}
JOptionPane.showMessageDialog(null, "You entered " + builder, "Value",
JOptionPane.INFORMATION_MESSAGE); // output box
}
}