【问题标题】:Save different variables from input dialog从输入对话框保存不同的变量
【发布时间】:2019-07-26 03:27:33
【问题描述】:

我是编程新手,目前正在尝试解决这个程序。应该有一个输入对话框,用户可以在其中输入不同的数字,直到用户按下“取消”按钮。

我使用do-while 循环在每次用户按下 OK 时弹出输入对话框,但如何保存用户输入的每个数字?

当用户按下 CANCEL 时,程序应显示一个消息对话框,其中应发布最高和最低数字。我现在的程序是这样的,

public class Övning5komma91 {

  public static void main(String[] args) {
    int knappNr;
    int n;
    String s;
    do {
        s = JOptionPane.showInputDialog("Skriv ett nummer");
        n = Integer.parseInt(s);
    } while (s != null && s.length() > 0);


  }

}

【问题讨论】:

  • 这不是真正的minimal reproducible example,但不清楚您在这里真正想做什么。如果您实际上有 GUI 组件和单击处理程序,那么这些处理程序应该调用执行您需要的代码。不过,目前还不清楚 OK 和 Cancel 处理程序之间的区别。想想处理数据的代码部分,以及显示结果和接受输入的部分。就目前而言,我认为这个问题对于 SO 来说有点过于宽泛了。
  • @jdv,您的说法并不完全正确。 JOptionPane.showInputDialog 是一个有效的 GUI 输入,它将返回输入的值或 null。 OP 的程序运行良好(嗯,除了用户取消的 NPE 和 null 的解析,但这是学习 Java 的一部分)。
  • 耸耸肩它仍然不是minimal reproducible example

标签: java variables save


【解决方案1】:

要保存所有数字,您需要将它们放入数据结构中。如果条目的数量是未知的先验,那么使用List<Integer> enteredNums = new ArrayList<>(); 然后if (s != null) { enteredNums.add(Integer.parseInt(s); } 将允许您累积每个输入的数字。

然后您可以通过以下方式处理输入的数字:

for (Integer num : enteredNums) {
   ...
}

根据评论编辑:

OP 提出了两个问题:

  1. 如何保存用户输入的每一个数字?
  2. 如何在消息对话框中发布最高和最低数字。

现在如果跟踪 #1,从逻辑上讲 #2 可以解决,但在评论中收集实际数据似乎不太重要。

所以,这是一种不同的方法。如果主要目标是跟踪最大和最小,并且所有输入值的累积并不是真正的要求),那么创建几个变量并有条件地设置它们将起作用。在每个条目之后,更新最小、最大和(在这里,为了好玩)总和。

没有数据结构(如列表)就不可能完成#1,但假设真正的愿望是#2,那么就没有必要跟踪所有输入,只需跟踪当前最小和最大的输入。

public static void main(String[] args) {
   // the smallest entered number
   int smallest = 0;

   // the largest entered number
   int largest = 0;

   // the total; since lots of ints could overflow an int, use a long; not
   // completely immune to overflow, of course
   long sum = 0L;

   // whether it is the first entered number
   boolean foundOne = false;

   int n;
   String s;
   do {
      // get an input
      s = JOptionPane.showInputDialog("Skriv ett nummer");

      // have to ensure not canceled; as s will be null
      if (s != null) {
        // parse the entered value; note this will throw an exception if
        //   cannot parse     
        n = Integer.parseInt(s);

        // on the first go through, we set unconditionally; also update
        //  the boolean so can indicate if anything was entered
        if (! foundOne) {
          smallest = n;
          largest = n;
          foundOne = true;
        }
        else {
          // set the smallest and the largest entered values
          //  the Math methods are just easier to read, but could
          //  be done by if statements
          smallest = Math.min(smallest, n);
          largest = Math.max(largest, n);
        }

        // add to the running total
        sum += n;
      }
} while (s != null && s.length() > 0);

// output the smallest, largest, total if anything entered
if (foundOne) {
  // this should be a dialog, rather than just output, but that is left
  //   as an exercise for the reader
  System.out.printf("The smallest value was %d\n", smallest);
  System.out.printf("The largest value was %d\n", largest);
  System.out.printf("The total of all entered values was %d\n", sum);
}
else {
  System.out.println("no values were entered");
}

}

注意:未测试

【讨论】:

  • 因为我是 Java 新手,所以我不太明白你写的什么哈哈。我正在按照一步一步的书,到目前为止,我只阅读了有关如何使用数字、字符串、if、while(do-while、break、for)的内容。你知道我如何用这些工具解决这个问题吗? :)
  • @RobinJörgensen,我做了一些编辑。您可以仅用数字解决问题的部分(尽管我在代码中使用了布尔值以允许在个人未输入任何内容时将其分开)。滥用String,可以累积每个输入的数字,然后将它们打印出来,但恕我直言,这种糟糕的方法不值得学习。因此,编辑基本上显示了如何跟踪最小和最大输入值。
  • 非常感谢!我了解您在该代码中写的内容:)
  • 很高兴它有帮助。请考虑接受答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 2013-03-28
相关资源
最近更新 更多