读取整数并找到最小值/最大值

时间:2011-09-18 15:27:26

标签: java eclipse

下面你会找到一个SSCCE。

我正在尝试输入一些整数,看看哪个是最大和最小的。

当我运行/调试它时,只有第一个整数将获得显示System.out.println()的代码.....

import java.util.Scanner;

public class Comparison 
{

    public static void main(String[] args) 
    {

        Scanner input = new Scanner ( System.in );
        int a; //1st #
        int b; //2nd #
        int c; //3rd #
        int d; //4th #
        int e; //5th #
        int large = 0; //Largest #
        int small = 0; //Smallest #

        System.out.print( "Enter the first integer: ");
        a = input.nextInt();

        System.out.print( "Enter the second integer: ");
        b = input.nextInt();

        System.out.print( "Enter the third integer: ");
        c = input.nextInt();

        System.out.print( "Enter the fourth integer: ");
        d = input.nextInt();

        System.out.print( "Enter the fifth integer: ");
        e = input.nextInt();



        if ( a>b )
        {   large = a;

        if ( a<b )
            small = a;

        if ( a>c )
            large = a;

        if ( a<c )
            small = a;

        if ( a>d )
            large = a;

        if ( a<d )
            small = a;

        if ( a>e )
            large = a;

        if ( a<e )
            small = a;


        if ( b>a )

            large = b;

        if ( b<a )
            small = b;

        if ( b>c )
            large = b;

        if ( b<c )
            small = b;

        if ( b>d )
            large = b;

        if ( b<d )
            small = b;

        if ( b>e )
            large = b;

        if ( b<e )
            small = b;


        if ( c>b )

            large = c;

        if ( c<b )
            small = c;

        if ( c>a )
            large = c;

        if ( c<a )
            small = c;

        if ( c>d )
            large = c;

        if ( c<d )
            small = c;

        if ( c>e )
            large = c;

        if ( c<e )
            small = c;


        if ( d>b )

            large = d;

        if ( d<b )
            small = d;

        if ( d>c )
            large = d;

        if ( d<c )
            small = d;

        if ( d>a )
            large = d;

        if ( d<a )
            small = d;

        if ( d>e )
            large = d;

        if ( d<e )
            small = d;


        if ( e>b )

            large = e;

        if ( e<b )
            small = e;

        if ( e>c )
            large = e;

        if ( e<c )
            small = e;

        if ( e>d )
            large = e;

        if ( e<d )
            small = e;

        if ( e>a )
            large = e;

        if ( e<a )
            small = e;

        System.out.println(large + " is the largest number you have given.");
        System.out.println(small + " is the smallest number you have given.");
        }

    }

}

4 个答案:

答案 0 :(得分:7)

这可能是因为你有:

 if ( a>b )
 {   large = a;
     ...

作为你的第一个if - 语句,你最后关闭(可能是因为编译器告诉你缺少}):

    System.out.println(large + " is the largest number you have given.");
    System.out.println(small + " is the smallest number you have given.");

    }  <------------

以下是两种替代解决方案:

....

large = Collections.max(Arrays.asList(a, b, c, d, e));
small = Collections.min(Arrays.asList(a, b, c, d, e));

....

和另一个使用TreeSet

Scanner input = new Scanner ( System.in );

TreeSet<Integer> integers = new TreeSet<Integer>();

System.out.print( "Enter the first integer: ");
integers.add(input.nextInt());

System.out.print( "Enter the second integer: ");
integers.add(input.nextInt());

System.out.print( "Enter the third integer: ");
integers.add(input.nextInt());

System.out.print( "Enter the fourth integer: ");
integers.add(input.nextInt());

System.out.print( "Enter the fifth integer: ");
integers.add(input.nextInt());

System.out.println(integers.last() + " is the largest number you have given.");
System.out.println(integers.first() + " is the smallest number you have given.");

TreeSet可以存储元素并轻松检索最大和最小的元素。

答案 1 :(得分:0)

代码不正确。该代码仅在&gt;时运行。 B:

if ( a>b )
    {   large = a;

此大括号仅在您的System.out.println语句之后关闭。

答案 2 :(得分:0)

好的,首先,你分配大小的逻辑是不对的。

您应该为大/小分配一个值,并对所有输入变量交叉检查大/小。

其次,这仅在(a > b)时有效,因为System.out.println()语句在该块中。

答案 3 :(得分:0)

aioobe的TreeSet解决方案非常棒!我也投了赞成票。对于初学者来说,我认为沿着这些方向的东西会更合适......

import java.util.Scanner;

public class Comparison {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int anum;
        int large = Integer.MIN_VALUE; //Largest #
        int small = Integer.MAX_VALUE; //Smallest #

        for (int j = 1; j < 6; j++) {
            System.out.print("Enter the " + j + " integer: ");
            anum = input.nextInt();
            if (anum < small) {
                small = anum;
            }
            if (anum > large) {
                large = anum;
            }
        }

        System.out.println(large + " is the largest number you have given.");
        System.out.println(small + " is the smallest number you have given.");
    }
}