2017-10-11 78 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 ++”增量,然后它应该通过删除未使用的字符串变量**消息**来清除代码。 –