gpt4 book ai didi

java - 询问用户是否想要提供多个输入

转载 作者:行者123 更新时间:2023-12-01 13:55:54 26 4
gpt4 key购买 nike

我正在做一个 Java 类介绍的作业,并且在处理用户需要提供多个输入的情况时遇到一些困难。问题给出如下:

“要求用户输入一个数字。您应该使用输入对话框进行此输入。请务必将对话框中的字符串转换为实数。程序需要跟踪用户的最小数字以及输入的最大数字。询问用户是否要输入其他数字。如果是,则重复该过程。如果否,则输出用户输入的最小和最大数字。

当用户想要退出时,该程序在程序结束时输出最大和最小的数字。

此外,您的程序应该考虑用户仅输入一个数字的情况。在这种情况下,最小和最大的数字将是相同的。”

我的问题是,我无法弄清楚如何让程序不断询问用户是否要输入另一个数字......与他们说"is"的次数一样多(显然)。我知道我必须使用循环或其他东西,但我是初学者,不知道从哪里开始。任何帮助将不胜感激,并提前致谢!

这是我到目前为止所拥有的:

包寻找minandmax;

导入javax.swing.JOptionPane;

公共(public)课Findingminandmax{

public static void main(String[] args)
{
String a = JOptionPane.showInputDialog("Input a number:");
int i = Integer.parseInt(a);

String b = JOptionPane.showInputDialog("Would you like to input another number? yes or no");

if ("yes".equals(b)) {
String c = JOptionPane.showInputDialog("Input another number:");
int j = Integer.parseInt(c);

int k = max(i, j);

JOptionPane.showMessageDialog(null, "The maximum between " + i +
" and " + j + " is " + k);

} else {
JOptionPane.showMessageDialog(null, "The maximum number is " + i );
}

}

public static int max(int num1, int num2) {
int result;

if (num1 > num2)
result = num1;
else
result = num2;

return result;
}

}

最佳答案

String b = JOptionPane.showInputDialog("Would you like to input another number? yes or no");
while(b.equalsIgnoreCase("yes")){
String c = JOptionPane.showInputDialog("Input another number:");

// your logic
b = JOptionPane.showInputDialog("Would you like to input another number? yes or no");
}
// then have your logic to print maximum and minimum number

但是要获得“是/否”输入,请使用确认对话框而不是输入对话框

例如

int b = JOptionPane.showConfirmDialog(null, "Would you like to input another number? yes or no", "More Inputs", JOptionPane.YES_NO_OPTION);
while (b == JOptionPane.YES_OPTION) {
// your logic
}

关于java - 询问用户是否想要提供多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19626599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com