【发布时间】:2020-05-11 16:05:30
【问题描述】:
public class NastingIfElse {
public static void main(String[] args) {
int a = 998 , b = 857 , c = 241 , d = 153;
int result;
if ( a > b ) {
if ( a > c ) {
if ( a > d ) {
result = a;
}else {
result = d;
}
}
}
if ( b > c ) {
if ( b > d ) {
result = b;
}else {
result = d;
}
}
if ( c > a ) {
if ( c > d ) {
result = c;
}else {
result = d;
}
}
if ( d > a ) {
if ( d > c ) {
if ( d > b ) {
result = d;
}else {
result = b;
}
}
}
System.out.println("Biggest number of three is " + result);
}
}
我想做这个代码并从这 4 个数字中找出最大的数字。 但是这个“嵌套if程序”有一些问题。 所以我需要找到一些方法来运行这个特定的程序,以便仅使用“嵌套 if”从这些数字中找到最高数字。
所以在这个程序中帮助我。
【问题讨论】:
-
您可以将所有内容替换为:
Math.max(Math.max(Math.max(a,b),c), d);如果您有任意数字列表,则可以在循环中执行此操作,Math.max(max_of_last_pair, current_number);。 -
"但是有一些问题" => 那你为什么不告诉我们你有什么问题呢?
-
顺便说一下,你的输出应该是
"Biggest number of four is "。 :) -
是不是“必须”通过嵌套的 if 和 else 来完成的课程练习?
-
是的,Majid Roustaei 上了一课。
标签: java class if-statement numbers nested-if