网店代刷:java问题:判断两个数的大小

来源:百度文库 编辑:高校问答 时间:2024/04/30 14:16:59
编写一个方法并调用它,求两个整数中的大者.
方法头:int max(int x,int y)
运行结果为:max(10,20)=> 20
我编的是:
public class Alone5_1{
int max(int x,int y){
if(x>y)max=x;
else max=y;
}
public static void main(String[] args){
Alone5_1 xy=new Alone5_1();
System.out.println("max(10,20) >= "+ xy.max(10,20));

}
}
请检查!

import java.util.Scanner;  
public class zuoye03_5_2 {  
    public static void main(String[] args) {  
        Scanner sc=new Scanner(System.in);  
        System.out.println("---->输入两个数(例如20 12):");  
        int max=sc.nextInt();  /*键盘输入*/   
        int min=sc.nextInt();     
        String a;  /*定义变量*/  
        a=(max>min)?"大":"小";  
        System.out.println(a);     /*输出最大值*/     
    }  
}

你明显受到了Pascal的影响。函数名不能作为变量回带数值。该为:
public class Alone5_1{
int max(int x,int y){
if(x>y)
return x;
else
return y;
}
public static void main(String[] args){
Alone5_1 xy=new Alone5_1();
System.out.println("max(10,20) >= "+ xy.max(10,20));

}
}

一楼说的很对,其实那样是没返回值得!另外,判断条件那最好改成:

if(x>y)
{return x; }
else
{return y; }

最近发现好多转语言的人啊。

楼上说的对