Java是一种非常流行的编程语言,它可以用来解决各种各样的问题,包括求两个数和三个数的最大值。下面我们将介绍如何使用Java编写程序来实现这个功能。
public class MaxNumber {public static int getMax(int a, int b) {return a >b ? a : b;}public static int getMax(int a, int b, int c) {int max = getMax(a, b);return max >c ? max : c;}public static void main(String[] args) {int num1 = 10;int num2 = 20;int num3 = 30;int max2 = getMax(num1, num2);int max3 = getMax(num1, num2, num3);System.out.println("两数最大值为:" + max2);System.out.println("三数最大值为:" + max3);}}
在这个程序中,我们定义了一个名为MaxNumber的类,并在其中定义了两个getMax()方法。第一个getMax()方法用于求两个数的最大值,第二个getMax()方法用于求三个数的最大值。在main()方法中,我们定义了三个整型变量,分别代表两个数和三个数。然后我们使用getMax()方法来求出它们的最大值,并在控制台输出结果。