JAVA 判断两个数大小 并输出最大值

详细步骤

public class Test {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("请输入2个整数:");
}
else {
try{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("你输入的是" + a + "和" + b);
System.out.println("最大值是:" + getMax(a, b));
} catch (Exception e) {
System.out.println("你输入的不是整数:");
}
}
}

public static int getMax(int a, int b) {
return a > b ? a:b;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-06-04
public class Test {
public static void main(String[] args) {
System.out.println(Math.max(3, 4));
}
}

因为不知道你比的是double型还int型,或者其他类型,所以把相关方法全贴这了。

static double max(double a, double b)
返回两个 double 值中较大的一个。
static float max(float a, float b)
返回两个 float 值中较大的一个。
static int max(int a, int b)
返回两个 int 值中较大的一个。
static long max(long a, long b)
返回两个 long 值中较大的一个。
第2个回答  2010-06-02
public void max(int a,int b) {
if (a>b) System.out.println(a);
else System.out.println(b);
}
第3个回答  2010-06-02
System.out.println("最大值为:" + a > b ? a : b);
第4个回答  2010-06-02
public max(int a, int b){
return a > b ? a : b;
}
相似回答