使用对话框Java查找最小和最大数字

时间:2014-03-21 18:32:44

标签: java

所以我的任务就是这个

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

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

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

如果用户只输入1个数字,我很难做到这一点,那么两个值都会被设置,但同时只要用户说“是”就会循环播放。

    public static void main(String[] args)
{      
    int maxNum = Integer.MAX_VALUE;
    int minNum = Integer.MIN_VALUE;
    String repeat;
    String firstResponseString = JOptionPane.showInputDialog("Enter a Number ");
    maxNum = Integer.parseInt(firstResponseString);
    minNum = Integer.parseInt(firstResponseString);
    String userRepeat = JOptionPane.showInputDialog(" Would you like to input another number? Y/N");
    while(userRepeat.equalsIgnoreCase("Y"))
    {
        String num1String = JOptionPane.showInputDialog("Enter a Numer: ");
        int num1 = Integer.parseInt(num1String);
        if(num1 > maxNum)
           {
               maxNum = num1;
           }
        if(num1 < minNum)
           {
               minNum = num1;
           }  
        userRepeat = JOptionPane.showInputDialog(" Would you like to input another number? Y/N"); 
    }
     JOptionPane.showMessageDialog(null, "Largest Number: " + maxNum + "\nSmallest Number: " + minNum, "Find Min and Max", JOptionPane.INFORMATION_MESSAGE);

}

我想我现在正在使用此代码

2 个答案:

答案 0 :(得分:0)

您可以在声明中设置int minNum = Integer.MIN_VALUEint maxNum = Ingerer.MAX_VALUE

答案 1 :(得分:0)

字符串重复没有用处,因为它没有在任何地方使用,但在声明中也没有必要分配maxNum = ...和minNum = ....因为你正在解析maxNum和minNum所以它们可以被识别通过JOptionpane。

相关问题