用java编写程序,输入四个整数,输出最大最小值要求只用四个ifelse语句
时间: 2023-04-08 12:00:47 浏览: 127
我可以回答这个问题。以下是用 Java 编写程序,输入四个整数,输出最大最小值,只使用四个 if-else 语句的代码:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int max, min;
if (a > b) {
if (a > c) {
if (a > d) {
max = a;
} else {
max = d;
}
} else {
if (c > d) {
max = c;
} else {
max = d;
}
}
} else {
if (b > c) {
if (b > d) {
max = b;
} else {
max = d;
}
} else {
if (c > d) {
max = c;
} else {
max = d;
}
}
}
if (a < b) {
if (a < c) {
if (a < d) {
min = a;
} else {
min = d;
}
} else {
if (c < d) {
min = c;
} else {
min = d;
}
}
} else {
if (b < c) {
if (b < d) {
min = b;
} else {
min = d;
}
} else {
if (c < d) {
min = c;
} else {
min = d;
}
}
}
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
}
}
```
阅读全文