Java 我错过了什么?与对话框循环
我不知道为什么这个代码不起作用 我假设在用户选择yes或no之后会出现另一个对话框,但是每当我运行程序时,它都会询问y或no,然后什么都不会发生 对我需要做什么有什么想法吗Java 我错过了什么?与对话框循环,java,Java,我不知道为什么这个代码不起作用 我假设在用户选择yes或no之后会出现另一个对话框,但是每当我运行程序时,它都会询问y或no,然后什么都不会发生 对我需要做什么有什么想法吗 public static void main(String[] args) { Scanner in = new Scanner(System.in); int max = 0; int min = Integer.MAX_VALUE; String number; bool
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = 0;
int min = Integer.MAX_VALUE;
String number;
boolean yn = true;
do {
number = JOptionPane.showInputDialog("Please enter a number");
int num = Integer.parseInt(number);
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
System.out.println(min + " " + max);
JOptionPane.showInputDialog("Would you like to enter another number? (y/n)");
String ny = in.nextLine();
if (ny.equals("n")) {
yn = false;
}
JOptionPane.showInputDialog(ny);
} while (yn == true);
JOptionPane.showMessageDialog(null, "The max number is " + max + " and the mininum number is " + min);
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int max=0;
int min=整数最大值;
字符串编号;
布尔值yn=真;
做{
number=JOptionPane.showInputDialog(“请输入一个数字”);
int num=Integer.parseInt(数字);
如果(数值>最大值){
max=num;
}
if(num
}程序将在
String ny = in.nextLine();
根据第一行定义的扫描仪等待System.in的输入
如果在控制台上输入“n”并按enter键,则程序将继续并显示下一个对话框
我猜你是想说:
String ny = JOptionPane.showInputDialog("Would you like to enter another number? (y/n)");
您遇到的问题是,您没有接受来自面板的输入,而是从控制台获取输入。要解决此问题,请将ny设置为等于来自JPane的输入,如下所示:
String ny = JOptionPane.showInputDialog("Would you like to enter another number? (y/n)");
但是,还有一个问题,那就是这一行:
JOptionPane.showInputDialog(ny);
它创建了一个您不需要的窗格,该窗格显示y或n,并且不接受输入。这条线不需要在那里,所以你应该删除它。否则,您的代码可以正常工作。它正在等待扫描仪的输入。我猜那不是你想要的。