不知道为什么我的while循环不工作(Java)。。?
我的代码的目的是输入一个pin,它会检查它是否正确。如果不是,问题就会循环。 由于某些原因,我的代码没有正确循环,很多代码都有下划线。特别是while循环本身和第二个JOptionPane不知道为什么我的while循环不工作(Java)。。?,java,loops,while-loop,Java,Loops,While Loop,我的代码的目的是输入一个pin,它会检查它是否正确。如果不是,问题就会循环。 由于某些原因,我的代码没有正确循环,很多代码都有下划线。特别是while循环本身和第二个JOptionPane // package loop; import javax.swing.JOptionPane; public class loop { public static void main(String[] args) { int correctPin = 3333; int coun
// package loop;
import javax.swing.JOptionPane;
public class loop {
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin);{
maybePin = JOptionPane.showInputDialog("Please enter the PIN");
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
}
看看这个;这就终止了循环。您需要删除该变量。您永远不会更新正在检查的变量sMabyPin。如果你按照@John和@ANS的建议去做,你将陷入一个无限循环。移除;在while语句和correct之后,将变量sMaybePin的值设置为输入vlue和ot works
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
删除分号:whilecorrectPin!=斯梅贝平{另外,我相信maybePin=JOptionPane.showInputDialog请输入PIN;while循环中的PIN是错误的。您需要将其分配给sMaybePin。您的另一个真正问题是sMaybePin永远不会更改其值,因为更改该字符串maybePin不会影响前面解析的int值。此外,您的名称只会增加混淆n在这里!嗯,这不起作用,我不能将maybePin改为sMaybe,因为sMaybe是一个int值int不能与JOptionPaneJOptionPane一起使用int返回一个字符串。我用Integer.parseInt将这个字符串转换为int。你的是一个认为int值需要重新解析的人!P1就是这个。错误。你注意到他正在比较的值了吗?他缺少reparse the int value!看起来你是第一个解决这个难题的人:-p1也是;尽管你的答案目前看起来更像是一个平庸的注释。
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}