JOptionPane多维数组输出 [英] JOptionPane Multidimensional Array output
问题描述
好吧,我有要在JOptionPane.showMessageDialog
中显示的多维数组.我知道使用System.out.println
时会使用for循环.但是,数组大小由用户输入确定,因此我必须使用增量器.
Ok say I have Multidimensional array that I want to display in JOptionPane.showMessageDialog
. I know that when using System.out.println
, you use a for loop. However the array size is determined by the user input, therefore I have to use a incrementor.
例如:usernumber[k]
旁边的userinput[k]
,那么下一行将是usernumber[k+1]
旁边的userinput[k+1]
For example: userinput[k]
next to usernumber[k]
then the next row would be userinput[k+1]
next to usernumber[k+1]
我遇到的麻烦是,通过使用循环,它每次都在单独的窗口中一次设置一个,而不是在一个窗口中的一个表中一起设置.
The trouble I am having is that by using my loop, it does each set one at a time in separate windows and not all together in a table in one window.
for (int k = 0; k < userinput.length; k++){
JOptionPane.showMessageDialog(null, userinput[k]);
}
for (int k = 0; k < 2; k++){
JOptionPane.showMessageDialog(null, usernumber[k]);
}
推荐答案
将输出构建为单个String
.
您可以使用html
标签提供其他格式
You can use html
tags to provide additional formatting
StringBuilder sb = new StringBuilder(64);
sb.append("<html><table>");
for (int ui = 0; ui = < userinput.length; ui++) {
sb.append("<tr><td>");
sb.append(userinput[ui]);
sb.append("</td>");
for (int k = 0; k < 2; k++){
sb.append("<td>");
sb.append(usernumber[k]);
sb.append("</td>");
}
sb.append("</tr>");
}
sb.append("</table></html>");
JOptionPane.showMessageDialog(null, sb.toString());
这篇关于JOptionPane多维数组输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!