该代码的主要目标是使用此关键字并设置全局变量(十,零和二十等于整数十,整数零,整数二十。)因此,我将调用该方法并将其加在一起。 (总价值为30)

package javaapplication53;

public class NewClass {

public int ten = 10;
public int zero = 0;
public int twenty = 20;

public int yourMethod(int ten, int zero, int twenty) {



    this.ten = ten;
    this.zero = zero;
    this.twenty = twenty;

   return(ten +zero+ twenty);
}
}


然后我在主方法中调用了构造函数。

   package javaapplication53;

    public class JavaApplication53 {


    public static void main(String[] args) {
    NewClass nc = new NewClass();
    nc.NewClass(ten, zero, twenty);
}


}

它说我必须输入3 int,我以为我在另一堂课上做了。

我是计算机编程的新手

最佳答案

您打算调用NewClass中定义的方法-

因此,而不是-

nc.NewClass();


您可能想要-

nc.yourMethod(n1, n2, n3); //where n1, n2, n3 are integers.


例-

System.out.println(nc.yourMethod(50, 45, 89));


另外,也许您希望NewClass像这样,因为将新值分配给方法参数不是一个好习惯:

public class NewClass {

    private int ten;
    private int zero;
    private int twenty;

    public int yourMethod(int ten, int zero, int twenty) {

        this.ten = ten;
        this.zero = zero;
        this.twenty = twenty;

        int sum = (this.ten + this.zero + this.twenty);

       return sum;
    }

}


如果您想避免不小心将新值分配给方法参数,可以使用final这样的方法,这是一个很好的做法-

public int yourMethod(final int ten, final int zero, final int twenty) {

    // code

}

08-07 05:48