2014-01-14 45 views
3

明天我有一項任務,我完成它,我只是不斷收到一個錯誤。那麼它不完全是一個錯誤,因爲它是我的輸出不是我的老師想要的輸出。使用JOptionPane創建乘法表

我的老師要我創建使用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

我的代碼不具有數字之間有適當的間距,我一直試圖以某種方式解決它了一段時間,沒有運氣。

+1

數字的字符串表示有不同的長度,所以你需要使用不同數量的每個空間。因此,很容易得到比教師更好的結果...... –

+0

JTextArea也以編程方式知道TAB – mKorbel

+0

無關:請學習java命名約定並堅持使用它們。 – kleopatra

回答

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: enter image description here

+0

嗨,謝謝你的回答,但每列之間的空間太大https://www.dropbox.com/s/m5fr1nm4hqttujw/Screen%20Shot%202014-01-14%20at%205.55.22%20PM.png – user3114770

+0

@ user3114770使用(關於我的例子)'jf。 setTabSize(1);'或其他一些數字 –

+0

@ user3114770標籤大小爲3-4應該足夠使用默認字體.. –

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); 

} 

結果:

Result

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);   
    } 
    } 

OUTPUT:

output

+0

感謝您的幫助,但數字仍然沒有對齊,因爲他們應該。 – user3114770

+0

@ user3114770完成:) - – Aarav

+0

@ user3114770您的意思是[https://www.dropbox.com/s/6gtexqoj3rs7xvl/IMG-20140114-WA0000.jpg](https://www.dropbox.com/s /6gtexqoj3rs7xvl/IMG-20140114-WA0000.jpg) – Aarav