Java 求三个整数的最大值

Java 求三个整数的最大值,java,if-statement,max,Java,If Statement,Max,我已经做了一个程序来找出给定三个数字中最大的一个。它适用于一位数,但不适用于三位数。为什么不呢 package practice; import java.util.Scanner; public class AllPractice { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); in

我已经做了一个程序来找出给定三个数字中最大的一个。它适用于一位数,但不适用于三位数。为什么不呢

package practice;
import java.util.Scanner;

public class AllPractice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        if(a > b) {
            if (a > c) {
                System.out.println("maximum of the given numbers "+a);
            }else {
                if (b > a) {
                    if (b > c) {
                        System.out.println("maximum of the given numbers "+b);
                    }
                }else {
                    System.out.println("maximum of the given numbers "+c);
                }
            }
        }
    }
}

代码不起作用,因为如果变量
a
小于
b
,则永远不会输入第一个条件


简单的单线解决方案/备选方案:

intmax=Collections.max(Arrays.asList(a,b,c));

只有当a大于b时,您的程序才会工作。如果您想使用简单的If-else,下面的代码将起作用

if(a>b && a>c )
    System.out.println("maximum of the given numbers "+a);
else if (b>a && b>c)
    System.out.println("maximum of the given numbers "+b);
else 
    System.out.println("maximum of the given numbers "+c);

输入失败的例子是什么?实际输出和预期输出是什么?不清楚“不工作”是什么意思。请添加成功和失败案例的输入和输出示例。@vsfDawg,我认为“不工作”是指
a
不打印,将数字添加到集合中,然后使用集合。max()您也可以使用
List.of(a,b,c)
。也可能
IntStream.of(a,b,c).max().orelsetrow()
。和
Math.max(a,Math.max(b,c))