使用joptionpane在java中执行while循环

3gtaxfhh  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(331)

我试图建立一个程序,将确定它是负还是正,到目前为止,它的罚款,但似乎我的代码不读取负数,只有正数,我找不到什么问题。

import javax.swing.JOptionPane;

public class EYYY {

    public static void main(String[] args) {    
        int positive=0;
        int negative=0;
        int num=0;
        int loop = 1;

        while(loop<=5){

            Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
            if(num<0)
                negative++;

            if(num>=0)
                positive++;
            loop++;
        }

         JOptionPane.showMessageDialog(null,"Negative numbers in the program: " + negative + "\nPositive numbers in the program: " + positive);

    }
}
o75abkj4

o75abkj41#

这个 num=0; 使 num 值始终为零和正,但需要存储 JOptionPanenum 每次迭代的变量 while-loop 要检查新输入值是否为负值或正值:

num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));

完整代码:

int positive = 0;
int negative = 0;
int num = 0;
int loop = 1;

while (loop <= 5) {

    num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
    if (num < 0)
        negative++;

    if (num >= 0)
        positive++;
    loop++;
}

JOptionPane.showMessageDialog(null,
        "Negative numbers in the program: " + negative + "\nPositive numbers in the program: " + positive);

相关问题