java按钮显示多行_java – 在Joptionpane中显示多行?

嗨我是这个论坛的新手,也是

java的新手,我需要一些帮助来介绍我的java作业.我完成了大部分逻辑.问题是要编写一个程序,显示100到1000之间的所有数字,每行10个,可以被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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值