gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:44:24 27 4
gpt4 key购买 nike

您将在下方找到一个 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.");
}

}

}

最佳答案

这可能是因为您有:

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

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

关于java - 读取整数并找到最小值/最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com