【问题标题】:The method in the type is not applicable for the arguments (int)类型中的方法不适用于参数(int)
【发布时间】:2013-10-17 06:03:48
【问题描述】:
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 个整数。 方法需要对象吗??

【问题讨论】:

  • 您已声明该方法采用零参数...

标签: java methods


【解决方案1】:

这个代码,无意冒犯,是完全错误的,所以我会建议一个替代方案。

这里的重点是我让方法接受两个整数,然后将它们返回给打印总和的 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;
    }

}

【讨论】:

  • @Ingo 查看编辑。由于在保持与原始版本相似与完全重组之间存在冲突,我在不同的修订版本之间跳跃。我现在应该安顿下来了。
  • 你怎么能返回s.th。当你说方法返回 void??
  • @MadScientisto 再次参考我之前的评论并刷新页面以查看我的最新(最终)版本。 main 不返回任何内容,而 int add( 现在在我修复了我带来的不一致之后返回。
【解决方案2】:

你希望你的方法得到两个整数,但是它没有得到任何“add()”。它应该是“add(int a, int b)”。

此外,这两个数字不应该是类的一部分。

【讨论】:

  • “此外,所有这些静态的东西都是多余的。”静态方法(add 和 void)根本不是多余的。你的意思是说这些字段是多余的?
  • @hexafraction 是 :) 感谢您的回答。我正在更新中
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多