这段代码该如何修改?

来源:百度知道 编辑:UC知道 时间:2024/06/30 19:43:08
class HelloWorld {
public static void main(String args[]) {
int a,b,c;
public int max(int a,int b)
{
if(a>b)
c=a;
else
c=b;
}
a=6;
b=7;
System.out.println(c);

}
}

class HelloWorld {

public static void main(String args[]) {
int a,b,c;
a=6;
b=7;
System.out.println(max(a,b));

}

public int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
}

上面兄弟这段代码是错的
java 是以对象为基础的,main方法是主方法,max方法必须属于一个类才能被调用,所以
class HelloWorld {

public static void main(String args[]) {
int a,b,c;
a=6;
b=7;
HelloWorld world = new HelloWorld();
System.out.println(world.max(a,b));

}

public int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
}

或者将max方法变成类方法
class HelloWorld {

public static void main(String args[]) {
int a,b,c;
a=6;
b=7;
System.out.println(max(a,b));

}

public static int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
}

输出最大的那个呢?