JAVA程序改错改错public class Modify5t { public static void pain(String [] args) {int a = 20;int b = 50;int c = sum(a, b);System.out.println("The sum is: " + c); } public int sum(int a, int b) {return a + b; }}急求!

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/29 05:12:35
JAVA程序改错改错publicclassModify5t{publicstaticvoidpain(String[]args){inta=20;intb=50;intc=sum(a,b);S

JAVA程序改错改错public class Modify5t { public static void pain(String [] args) {int a = 20;int b = 50;int c = sum(a, b);System.out.println("The sum is: " + c); } public int sum(int a, int b) {return a + b; }}急求!
JAVA程序改错改错
public class Modify5t {
public static void pain(String [] args) {
int a = 20;
int b = 50;
int c = sum(a, b);
System.out.println("The sum is: " + c);
}
public int sum(int a, int b) {
return a + b;
}
}
急求!

JAVA程序改错改错public class Modify5t { public static void pain(String [] args) {int a = 20;int b = 50;int c = sum(a, b);System.out.println("The sum is: " + c); } public int sum(int a, int b) {return a + b; }}急求!
public static void pain(String [] args) {
public static void main(String [] args) {
细心很重要!不过问题还没解决、、、
解决方案:
第一种方法
sum 方法前面加上static关键字,一定行的.因为main是方法静态方法,静态方法只能调用静态成员.
第二种方法
静态方法想调用实例成员的话,就在main方法里面实例化一个类对象,通过对象名调用sum方法.
public class Modify5t {
public static void pain(String [] args) {
Modify5t M=new Modify5t();
int a = 20;
int b = 50;
int c =M. sum(a, b);
System.out.println("The sum is: " + c);
}
public int sum(int a, int b) {
return a + b;
}
}