三个数最值编程怎么写

时间:2025-03-05 11:24:36 明星趣事

Java:

```java

public class MaxOfThreeNumbers {

public static void main(String[] args) {

int a = 4;

int b = 2;

int c = 3;

int max = 0;

if (a > b && a > c) {

max = a;

} else if (b > a && b > c) {

max = b;

} else {

max = c;

}

System.out.println("max = " + max);

}

}

```

C++:

```cpp

include

using namespace std;

int main() {

int a, b, c;

cout << "请输入三个整数,用空格分隔: ";

cin >> a >> b >> c;

int max = a;

if (b > max) {

max = b;

}

if (c > max) {

max = c;

}

cout << "最大值是: " << max << endl;

return 0;

}

```

Python:

```python

输入三个整数

num1 = int(input("请输入第一个整数: "))

num2 = int(input("请输入第二个整数: "))

num3 = int(input("请输入第三个整数: "))

比较三个数,找出最大值

max_value = max(num1, num2, num3)

输出最大值

print("最大值是:", max_value)

```

这些代码分别演示了如何在Java、C++和Python中通过条件判断找出并输出三个数中的最大值。你可以根据自己的编程语言选择合适的代码进行实现。