嗨新來這個論壇,也是新來的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 + " "));
}
}
}
}
**注意**在我的解決方案,我做字符串連接循環效率不高的表現明智內,但因爲它是執行我做這樣的一個程序。對於生產級代碼,不應該遵循這種字符串連接方法。 –
首先感謝你的解決方案!但是,當我運行你的解決方案時,第一行有9個答案,而我需要在一行中有10個答案。 –
只需刪除** for循環**開始處的第一個「count ++」增量,然後它應該通過刪除未使用的字符串變量**消息**來清除代碼。 –