嘿,我的方法不起作用,“方法 ___ 未定义类型 ___”,即使我尝试复制工作方法

问题描述 投票:0回答:1

公开课 Mehtodentest {

public static void main(String[] args) {
    // TODO Auto-generated method stub



    public static int calculator(int zahl1, int zahl2, char operator) {
        
        int sum;
        if (operator = '+') {
            sum = zahl1+ zahl2;
        } 
        else { sum = zahl1 - zahl2;
        }
        return sum;
    }

    int summeR = calculator(5,5,'+');
    System.out.println("..." + sumR);


}

}

线程“main”java.lang.Error中出现异常:未解决的编译问题: Mehtodentest 类型的方法 taschenrechner(int, int, char) 未定义 标记“int”上的语法错误,预期记录 参数 taschenrechner 的修饰符非法;只允许最终的 标记“;”上的语法错误,{ 在此标记之后应预期 语法错误,请插入“}”来完成RecordBody

at Mehtodentest.main(Mehtodentest.java:11)

picture of the code

我尝试让一个方法工作,但出乎意料的是它没有工作,所以我创建了一个新类(没有很多可能成为问题的代码)来测试那里的工作方法,但它没有工作,即使我之前看到它工作。

java methods undefined
1个回答
1
投票

不能在方法内定义方法。

public class Mehtodentest {
   public static int calculator(int zahl1, int zahl2, char operator) {
        
        int sum;
        if (operator == '+') {
            sum = zahl1+ zahl2;
        } 
        else { sum = zahl1 - zahl2;
        }
        return sum;
    }
  public static void main(String[] args) {
    int summeR = calculator(5,5,'+');
    System.out.println("..." + sumR);
  }
}

正确缩进并组织你的代码,它就会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.