繁体   English   中英

类型中的方法不适用于参数(int)

[英]The method in the type is not applicable for the arguments (int)

package com.testo.project1;

public class mathe {
    static int zahl1;
    static int zahl2;
    static int erg;

    public static void main(String[] args) {

        add(5,4);       

    }

    public static void add(){

        zahl1 = this.zahl1;
        zahl2 = this.zahl2;
        erg=zahl1+zahl2;
        System.out.println(erg);
    }




}

为什么此代码不起作用? 调用该方法时有2个整数。 该方法需要一个对象吗?

这段代码没有冒犯性,完全是错误的,因此我建议您使用另一种方法。

这里的要点是,我要让该方法接受两个int,然后将它们返回到打印总和的main。

package com.testo.project1;

public class mathe {
    public static void main(String[] args) {
        System.out.println(add(5,4));       
     }
     public static int add(int i1, int i2){

        return i1+i2;
    }

}

您希望您的方法获得两个整数,但是它不会获得任何“ add()”。 它应该是“ add(int a,int b)”。

另外,这两个数字不应该作为课程的一部分。

add方法中没有两个参数。 为该方法定义两个int参数。 另外,这些static变量不是必需的。 只需声明erg为和的局部变量即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM