-3
我有問題顯示我的輸出到一個for循環內的消息對話框。它目前在單獨的框中顯示每個輸出。目標是:在詢問用戶兩個整數之後,它應該在這些數字之間顯示一個素數和一個迴文的消息框。如:JAVA:For循環和JOptionPane
「的數字X和Y之間(這裏素數)
迴文(迴文這裏素)」
我已經試過StringBuilder的,但我是不是真的成功。任何想法和提示都會有所幫助。這裏是一個剪斷
for (int i = x; i <= y; i++) {
int k;
for (k = 2; k < i; k++) {
int n = i % k;
if (n == 0)
break;
}
if (i == k) {
JOptionPane.showMessageDialog(null, "Between the numbers " + x + " and " + y + ": ");
String output = i + "";
//this is where my issues start
//JOptionPane.showMessageDialog(null, output, "Prime Numbers and Palindromes", JOptionPane.INFORMATION_MESSAGE);
}
}
這是_expected_輸出。因爲,當if(i == k)變爲true時,你試圖在for循環中顯示消息框,它將開始顯示消息框,直到for-loop或條件變爲false。 – Shashanth