2017-10-11 77 views
4

嗨新來這個論壇,也是新來的java,我需要一些幫助我的介紹到java作業。我完成了大部分邏輯。問題是編寫一個程序,顯示100到1000之間的所有數字,每行10個,可以被5和6整除。數字間隔一個空格。我的教授希望它能在Joptionpane窗口中完成。當我嘗試這樣做時,窗口中只會彈出一個答案。如何讓我的答案在一行中顯示十位,僅在一個窗口中分隔一個空格?我的教授希望我們使用轉義函數來顯示答案。在Joptionpane中顯示多行?

public class FindFactors { 
    public static void main(String[] args) { 
     String message = ""; 
     final int NumbersPerLine = 10; // Display 10 numbers per line 
     int count = 0; // Count the number of numbers divisible by 5 and 6 

     // Test all numbers from 100 to 1,000 

     for (int i = 100; i <= 1000; i++) { 
      // Test if number is divisible by 5 and 6 
      message = message + " " + i; 
      count++; 
      if (count == 10) { 
       message = message + "\r\n"; 
       count = 0; 
      } 
      if (i % 5 == 0 && i % 6 == 0) { 
       count++; // increment count 
       // Test if numbers per line is 10 
       if (count % NumbersPerLine == 0) 
        JOptionPane.showMessageDialog(null, i); 
       else 
        JOptionPane.showMessageDialog(null, (i + " ")); 
      } 
     } 
    } 
} 

回答

0

一個JOptionPane具有顯示圖像,文本或任何其它組件,您可能希望創建自己的JPanel這增加了數字的每一行成JLabel這種特殊情況的能力,然後添加JLabel它。

import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class MultiLineOptionPane { 
    private JPanel pane; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new MultiLineOptionPane()::createAndShowGui); 
    } 

    public void createAndShowGui() { 
     pane = new JPanel(); 
     pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); 

     StringBuilder sb = new StringBuilder(); //This object will be used to concatenate in the next for loop 
     for (int i = 0; i < 500; i++) { //Sample for loop 
      if ((i) % 10 == 0) { //Every 10 numbers we restart the StringBuilder 
       pane.add(new JLabel(sb.toString())); //We add a new JLabel to the JPanel with the contents of the StringBuilder 
       sb.delete(0, sb.length()); //We restart the StringBuilder 
      } else { 
       sb.append(i); //We append the current number to the StringBuilder 
       sb.append(" "); //We append a space after the number 
      } 
     } 
     pane.add(new JLabel(sb.toString())); //We add the last line of numbers in the StringBuilder to the pane 

     JOptionPane.showMessageDialog(new JFrame(), pane, "Numbers", JOptionPane.PLAIN_MESSAGE); //We display the message 
    } 
} 

的輸出上面的程序看起來像(裁剪或那就太高):

enter image description here

1

請參閱下面的方法,稍有改變了代碼,並會給要求的輸出。

public class FindFactors { 
public static void main(String[] args) { 
final int NumbersPerLine = 10; // Display 10 numbers per line 
int count = 0; // Count the number of numbers divisible by 5 and 6 
// Test all numbers from 100 to 1,000 
String numbersPerLine = ""; 
for (int i = 100; i <= 1000; i++) { 
    // Test if number is divisible by 5 and 6 
    if (count == 10) {   
     count = 0; 
    } 
    if (i % 5 == 0 && i % 6 == 0) { 
     numbersPerLine =numbersPerLine+" "+i; 
     count++; // increment count 
     // Test if numbers per line is 10 

     if (count % NumbersPerLine == 0) 
      numbersPerLine =numbersPerLine+"\n"; 
    } 
} 
JOptionPane.showMessageDialog(null, numbersPerLine); 
} 
} 
+0

**注意**在我的解決方案,我做字符串連接循環效率不高的表現明智內,但因爲它是執行我做這樣的一個程序。對於生產級代碼,不應該遵循這種字符串連接方法。 –

+0

首先感謝你的解決方案!但是,當我運行你的解決方案時,第一行有9個答案,而我需要在一行中有10個答案。 –

+0

只需刪除** for循環**開始處的第一個「count ++」增量,然後它應該通過刪除未使用的字符串變量**消息**來清除代碼。 –