首页 文章

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

提问于
浏览
0

我正在为一个java类的介绍工作,并且在用户需要提供多个输入时遇到一些困难 . 问题如下:

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

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

此外,您的程序应考虑用户只输入一个号码的情况 . 在这种情况下,最小和最大的数字将是相同的 . “

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

这是我到目前为止:

包findminandmax;

import javax.swing.JOptionPane;

公共课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;
}

}

2 回答

  • 1
    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
    }
    
  • 0
    while(true) {
        //do stuff
        if ("yes".equals(b)) {
            //do other stuff
        } else { break; }
    }
    

相关问题