开发者

Reading integers and finding minimum / maximum values

Below you'll find an SSCCE.

I am trying to get a few integer's entered and see which is largest and smallest.

When I run/debug it only the first integer will get the code to show the 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 inte开发者_JAVA百科ger: ");
        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.");
        }

    }

}


This is probably due to the fact that you have:

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

as your first if-statement, which you close in the end (presumably because the compiler told you that there was a } missing):

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

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

Here are two alternative solutions for you though:

....

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

....

and another using 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.");

A TreeSet can store elements and easily retrieve the largest and smallest elements.


The code is not correct. The code will only be run if a > b:

if ( a>b )
    {   large = a;

This brace is only closed at after your System.out.println statements.


Ok, firstly, the your logic of assigning large and small isn't right.

You should assign a value to large/small , and cross check large/small against all the input variables.

Secondly, this would only work when (a > b) because the System.out.println() statements are in that block.


aioobe's TreeSet solution is great! I upvoted too. For starters though, i think something along these lines would fit better...

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.");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜