使用 JOptionPane 在 Java 中执行 while 循环
Do while loop in Java using JOptionPane
我正在尝试构建一个程序来确定它是负数还是正数,到目前为止还不错,但我的代码似乎不读取负数,只读取正数,我找不到什么错了。
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);
}
}
num=0;
导致 num
值始终为零和正数,但您需要在 num
的每次迭代中将 JOptionPane
的输入值存储在 num
变量中=16=] 检查新的输入值是负值还是正值:
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);
我正在尝试构建一个程序来确定它是负数还是正数,到目前为止还不错,但我的代码似乎不读取负数,只读取正数,我找不到什么错了。
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);
}
}
num=0;
导致 num
值始终为零和正数,但您需要在 num
的每次迭代中将 JOptionPane
的输入值存储在 num
变量中=16=] 检查新的输入值是负值还是正值:
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);