明天我有一个作业,我完成了,我只是一个错误。好吧,它不是一个错误,因为它是我的输出不是我的老师想要的输出。
我的老师要我使用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
我的代码在数字之间没有正确的间距,我一直试图以某种方式修复它而没有运气。
答案 0 :(得分:1)
您是否尝试过使用String.format("%5d", j)
?
它将在5个空格 - 长度上打印每个int。
示例:10
将为___10
。
或String.format("%-5d", j)
与左对齐。
示例:10___
。
修改:另一种选择是使用html
:
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 = "<html><table>";
int x = 1;
while (x <= X){
Result += "<tr>";
for(int i = 1;i<= Y; i++ ){
j = i * x;
Result += String.format("<td><center>%d</center></td>", j);
}
x++;
Result = Result + "</tr>";
}
Result += "</table></html>";
JOptionPane.showMessageDialog(null, Result);
}
<强>结果:强>
答案 1 :(得分:1)
一种解决方案(假设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:强>
答案 2 :(得分:1)
解决
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 = " |"+"\t";
int x = 1;
for(int i=1;i<=Y;i++){
Result = Result + i + "\t";
}
Result=Result+"\n";
while (x <= X){
if(x<10){
Result=Result+ x + " |" +"\t";
}else{
Result=Result+ x + "|" +"\t";
}
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);
}
}
输出: