从用户多次获取最大和最小五个输入

时间:2022-10-03 09:10:05
package com.company;

import java.util.Scanner;

class Compare
{
    Integer max;
    Integer min;

    public void max(int num)
    {
        if (max == null)
            max = num;
        else if(num > max)
            max = num;
    }

    public void min(int num)
    {
        if (min == null)
            min = num;
        else if(num < min)
            min = num;
    }
}
class Main
{
    public static void main(String args[])
    {
        Compare compare = new Compare();
        Scanner input = new Scanner(System.in);

        int sets = input.nextInt();
        for (int i = 0; i < sets ; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                int num_main = input.nextInt();
                compare.max(num_main);
                compare.min(num_main);
            }
            System.out.println(compare.max);
            System.out.println(compare.min);
        }
    }
}

I want to calculate max and min of five inputs from the user, i managed to get max value and min value but if i test two groups i dont get the correct max value instead i am getting the previous max value as my max value. How can i get the max value in the simplest possible way

我想计算来自用户的五个输入的最大值和最小值,我设法得到最大值和最小值但是如果我测试两个组我没有得到正确的最大值而不是我得到前一个最大值作为我的最大值。如何以最简单的方式获得最大值

2 个解决方案

#1


0  

The smallest change is to move the compare inside the first loop, so it finds the new max (and min) each time a new "set" of numbers is entered.

最小的变化是在第一个循环内移动比较,因此每次输入新的“数字”数字时,它会找到新的最大值(和最小值)。

for (int i = 0; i < sets ; i++)
{
    Compare compare = new Compare();
    //...
    //as you were     
}

When you make a compare before this loop it will keep hold of the values over each set of numbers entered. You want it to find the max and min per set of numbers entered.

在此循环之前进行比较时,它将保持输入的每组数字的值。您希望它找到输入的每组数字的最大值和最小值。

#2


0  

You have to restore your compare object before move to next loop. Modify a bit in your code:

在移至下一个循环之前,您必须恢复比较对象。修改代码中的一点:

 for (int i = 0; i < sets ; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            int num_main = input.nextInt();
            compare.max(num_main);
            compare.min(num_main);
        }
        System.out.println(compare.max);
        System.out.println(compare.min);
        compare = new Compare();
    }

Explain: Assume that sets = 2: After having gotten 5 user input, you get max and min. Before move to second loop, you must restore your compare object before getting 5 next user inputs as above code. you also can do:

解释:假设sets = 2:在获得5个用户输入后,您获得最大值和最小值。在转到第二个循环之前,必须先恢复比较对象,然后再获得5个下一个用户输入,如上面的代码。你也可以这样做:

for (int i = 0; i < sets ; i++)
{
    for (int j = 0; j < 5; j++)
    {
        int num_main = input.nextInt();
        compare.max(num_main);
        compare.min(num_main);
    }
    System.out.println(compare.max);
    System.out.println(compare.min);
    compare.max = compare.min = null;
}

#1


0  

The smallest change is to move the compare inside the first loop, so it finds the new max (and min) each time a new "set" of numbers is entered.

最小的变化是在第一个循环内移动比较,因此每次输入新的“数字”数字时,它会找到新的最大值(和最小值)。

for (int i = 0; i < sets ; i++)
{
    Compare compare = new Compare();
    //...
    //as you were     
}

When you make a compare before this loop it will keep hold of the values over each set of numbers entered. You want it to find the max and min per set of numbers entered.

在此循环之前进行比较时,它将保持输入的每组数字的值。您希望它找到输入的每组数字的最大值和最小值。

#2


0  

You have to restore your compare object before move to next loop. Modify a bit in your code:

在移至下一个循环之前,您必须恢复比较对象。修改代码中的一点:

 for (int i = 0; i < sets ; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            int num_main = input.nextInt();
            compare.max(num_main);
            compare.min(num_main);
        }
        System.out.println(compare.max);
        System.out.println(compare.min);
        compare = new Compare();
    }

Explain: Assume that sets = 2: After having gotten 5 user input, you get max and min. Before move to second loop, you must restore your compare object before getting 5 next user inputs as above code. you also can do:

解释:假设sets = 2:在获得5个用户输入后,您获得最大值和最小值。在转到第二个循环之前,必须先恢复比较对象,然后再获得5个下一个用户输入,如上面的代码。你也可以这样做:

for (int i = 0; i < sets ; i++)
{
    for (int j = 0; j < 5; j++)
    {
        int num_main = input.nextInt();
        compare.max(num_main);
        compare.min(num_main);
    }
    System.out.println(compare.max);
    System.out.println(compare.min);
    compare.max = compare.min = null;
}