简体   繁体   English

如何使用多个输入对话框(Java的新手)

[英]How to use multiple Input Dialogs (New to Java)

I am trying to create a program that asks a user for a sentinel value (a value to enter when they want to end the list). 我正在尝试创建一个程序,要求用户提供前哨值(当他们想结束列表时输入的值)。 It then asks the user to enter numbers until they re-enter the sentinel value. 然后,它要求用户输入数字,直到他们重新输入前哨值。 It then figures out the max number in the list. 然后,它计算出列表中的最大数量。 I'm very new to Java, and whenever I run the program is just asks for the sentinel value then does nothing else (never pops up the second input dialog). 我对Java还是很陌生,每当我运行该程序时,便会询问哨兵值,然后什么也不做(永远不会弹出第二个输入对话框)。 I'm sure it's something simple that I'm doing wrong, but I can't figure it out. 我敢肯定,我做错了很简单,但是我无法弄清楚。 Thanks for any help. 谢谢你的帮助。

import java.util.*; 

import javax.swing.JOptionPane;
public class HW1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    int number;
    int max;
    int sentinel;
    int count=0;

    JOptionPane.showInputDialog("Please enter a sentinel value: ");
    sentinel=input.nextInt();

    JOptionPane.showInputDialog("Please enter numbers. Enter" + sentinel +" to end.");
    number = input.nextInt();
    max = number;
    while (number!=sentinel){
        count +=1;
        if (number>max)
            max=number;
        JOptionPane.showInputDialog("Please enter numbers. Enter" + sentinel +" to end.");
        number = input.nextInt();
    }
    if (count!=0){
        JOptionPane.showMessageDialog(null, "The max is:" + max);
    }
  }
 }

I think the confusion is this: 我认为这是混乱的:

  • the JOptionPane opens with an input dialog JOptionPane打开并显示一个输入对话框

  • when the option pane closes, whatever you put there is ignored 当选项窗格关闭时,您放置在此处的任何内容都将被忽略

  • then the code goes to this line sentinel=input.nextInt(); 然后代码转到此行sentinel=input.nextInt();

    which waits for input from the console (eg you need to go back to the console, type the number there and press enter, only then the program will advance, it will block untill you do) 它等待来自控制台的输入(例如,您需要返回到控制台,在其中键入数字,然后按Enter键,然后程序才会前进,直到您执行该操作为止)

I would change it to something like this: 我将其更改为以下内容:

  String sentinelInput = JOptionPane.showInputDialog("Please enter a sentinel value: ");
  sentinel= Integer.parseInt(sentinelInput);

(repeat for all places where you expect input) (在所有您希望输入的地方重复)

An alternative solution is 替代解决方案是

Don't use the JOptionPane, and instead just System.out.println to print the user a request for input (instead of the popup dialog). 不要使用JOptionPane,而要使用System.out.println来向用户输出输入请求(而不是弹出对话框)。 Then you can and keep the existing input.nextInt() calls to collect it. 然后,您可以保留现有的input.nextInt()调用以进行收集。

Just note that all interaction will be in the console, without any popup dialogs (which I actually prefer in terms of user experience, and also it will be working in non GUI machines such as a linux terminal...) 请注意,所有交互都将在控制台中进行,而没有任何弹出对话框(就用户体验而言,这实际上是我更喜欢的对话框,并且它将在非GUI机器(例如linux终端)中正常工作...)

You are mixing the ways to input data to your program. 您正在混合将数据输入到程序中的方式。 Let's begin: 让我们开始:

Scanner input = new Scanner(System.in);

The line above allows you to catch data in the command line from the keyboard. 上面的行允许您从键盘上捕获命令行中的数据。

JOptionPane.showInputDialog("Please enter a sentinel value: ");

This Option Pane is showing correctly, you put a value and then nothing happens. 此选项窗格显示正确,您输入一个值,然后什么也没有发生。 This is because your program is waiting to input something in the command line 这是因为您的程序正在等待在命令行中输入内容

sentinel=input.nextInt();

When your program arrives to the line above, the input.nextInt() stops the program until you put something in the command line. 当您的程序到达上面的input.nextInt()input.nextInt()停止程序,直到您在命令行中放置了一些内容。

The correct way should be something like this: 正确的方法应该是这样的:

sentinel = Integer.parseInt(JOptionPane.showInputDialog("Please enter a sentinel value: "));
number = Integer.parseInt(JOptionPane.showInputDialog("Please enter numbers. Enter" + sentinel +" value to end."));

And remove: 并删除:

number = input.nextInt();
sentinel=input.nextInt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM