可以在JOptionPane.showMessageDialog中使用for循环吗?

时间:2015-12-04 19:31:53

标签: joptionpane

所以我是新来的,这是我的第一个问题......在JOptionPane.showMessageDialog中使用for循环是否有正确的语法?

这是我目前的代码,我知道它不起作用。我的代码用于显示某个整数的因子,我想知道如何在JOptionPane中显示它。

String c = JOptionPane.showMessageDialog(null
,for(d=1;d<=c;d++){
   if(c%d==0){
     d+" "
   }
 }  
,"The factors of "+c+" are: "
,JOptionPane.INFORMATION_MESSAGE);

1 个答案:

答案 0 :(得分:0)

谢谢Petter Friberg给出答案,感谢你找到了我的问题的解决方案,虽然我没有使用你的所有建议。所以,这是我的代码......

import java.util.*;
import javax.swing.*;
public class TestNo3{
    public static void main(String[] args) {
      JTextArea factorsArea = new JTextArea();
      JTextField inputInt1 = new JTextField(5);
      JPanel factorsPanel = new JPanel();
        factorsPanel.add(new JLabel("Enter an integer: "));
        factorsPanel.add(inputInt1);

     int int1 = JOptionPane.showConfirmDialog(null, factorsPanel
        ,"Show the factors of a number."
        ,JOptionPane.OK_CANCEL_OPTION);
          if(int1==JOptionPane.OK_OPTION){
            String inputIntS = inputInt1.getText();
            int inputIntI = Integer.parseInt(inputIntS);

            for(int numB=1;numB<=inputIntI;numB++){
              if(inputIntI%numB==0){
                String outputS = String.format("%5d", numB); 
                factorsArea.append(outputS);        
              }
            }

            JOptionPane.showMessageDialog(null, factorsArea
                ,"The factors of "+inputIntI+" are: "
                ,JOptionPane.INFORMATION_MESSAGE);      
          }
    }
}