我有一个明天要交的作业,我完成了它,但我总是出错。嗯,这不完全是错误,因为我的输出不是我老师想要的输出。
我的老师要我使用 JOptionPane
创建一个乘法表
这就是我目前所拥有的:
import javax.swing.JOptionPane;
public class assign3 {
public static void main(String[] args) {
String Boundary1 = JOptionPane.showInputDialog(null, "Please enter the first boundary of the multiplication table.");
String Boundary2 = JOptionPane.showInputDialog(null, "Please enter the second boundary of the multiplication table.");
int X = Integer.parseInt(Boundary1);
int Y = Integer.parseInt(Boundary2);
int j = 1;
String Result = "";
int x = 1;
while (x <= X){
for(int i = 1;i<= Y; i++ ){
j = i * x;
Result = Result + j + " ";
}
x++;
Result = Result + "\n";
}
JOptionPane.showMessageDialog(null, Result);
}
}
我的输出显示如下。
https://www.dropbox.com/s/ulp1gj9sqi94d3a/IMG_20140114_162054.jpg
但是我的老师希望输出像这样显示。
https://www.dropbox.com/s/6gtexqoj3rs7xvl/IMG-20140114-WA0000.jpg
我的代码在数字之间没有正确的间距,我一直试图以某种方式修复它,但没有成功。
最佳答案
一个解决方案(假设 result
已经有需要的制表符(\t
)):
JTextArea jf=new JTextArea(result);
jf.setEditable(false);
jf.setOpaque(false);
JOptionPane.showMessageDialog(null, jf);
编辑:
public static void main(String[] args) {
String Boundary1 = JOptionPane.showInputDialog(null, "Please enter the first boundary of the multiplication table.");
String Boundary2 = JOptionPane.showInputDialog(null, "Please enter the second boundary of the multiplication table.");
int X = Integer.parseInt(Boundary1);
int Y = Integer.parseInt(Boundary2);
int j = 1;
String Result = "";
int x = 1;
while (x <= X) {
for (int i = 1; i <= Y; i++) {
j = i * x;
Result = Result + j + "\t";
}
x++;
Result = Result + "\n";
}
JTextArea jt=new JTextArea(Result);
jt.setEditable(false);
jt.setOpaque(false);
jt.setTabSize(3);
JOptionPane.showMessageDialog(null, jt);
}
O/P:
关于java - 使用 JOptionPane 创建乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21113569/