建站知识
java求五个数的最大值和最小值
2024-12-26 18:13  点击:3

Java是一种非常常用的编程语言,也被广泛应用于各种软件开发和实践中。在Java中,我们可以使用一些简短的代码来求取一组数中的最大值和最小值。

//定义五个数int a = 20;int b = 10;int c = 8;int d = 4;int e = 40;//求取最大值int max = a;if (b >max) {max = b;}if (c >max) {max = c;}if (d >max) {max = d;}if (e >max) {max = e;}//求取最小值int min = a;if (b< min) {min = b;}if (c< min) {min = c;}if (d< min) {min = d;}if (e< min) {min = e;}//输出结果System.out.println("最大值为:" + max);System.out.println("最小值为:" + min);

上述代码中,我们首先定义了五个数a、b、c、d、e。然后通过比较,逐个判断这五个数中的最大值和最小值,并将结果输出。这个过程中,我们使用了if语句进行判断,通过不断对比与更新最大值和最小值,最终得到了结果。