java - 在 Joptionpane 中显示多行?

标签 java

您好,我是这个论坛的新手,也是 java 的新手,我需要一些帮助来介绍 java 作业。我完成了大部分逻辑。问题是编写一个程序,显示从 100 到 1000 的所有数字,每行十个,可以被 5 和 6 整除。数字之间只用一个空格分隔。我的教授希望它在 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 + " "));
            }
        }
    }
}

最佳答案

请参阅下面的方法,对您的代码稍作更改,将提供所需的输出。

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

关于java - 在 Joptionpane 中显示多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679398/

相关文章:

java - 响应时间随着 Java 中并发性的增加而增加

java - Java 中的 GUI 控制流和回调

java - 将外部类中的新数据插入到 Jtable 中

java - 将字符串数组传输到二叉树

java - 尝试使用 Alfresco Maven SDK 打包时出现 "Could not import bootstrap model"

java - AWS ResetException - 无法重置请求输入流

java - 无法运行 Jasper 报告 : The system cannot find the file specified

java - 为什么 httpclient 在 DO droplet 上有不同的行为(与 SSL 相关)?

java - [JAVA]递归删除扫雷中的空单元格

java - 调试从 JAVA 程序调用的 MATLAB 函数