简体   繁体   English

为什么我传递参数时它是无效的方法声明?

[英]Why is it an invalid method declaration when I am passing parameters?

    public class Store {
  // instance fields
  int area; 

  // constructor method
  public Calc(int one, int two, int three) {
    area = one*two*three;  
  }

  // main method
  public static void main(String[] args) {
    int sideOne = 2;
    int sideTwo = 3;
    int sideThree = 1;

    Calc mult = new Calc(sideOne,sideTwo,sideThree);

    System.out.println(mult.area);
  }
}

Can anyone help a beginner understand why, when passing parameters, this is an invalid method declaration?谁能帮助初学者理解为什么在传递参数时,这是一个无效的方法声明?

You define/call a Calc constructor, but there is no Calc class.您定义/调用Calc构造函数,但没有Calc类。

Rename your class to Calc ant your code will compile and execute correctly:将您的类重命名为Calc ant 您的代码将正确编译和执行:

public class Calc {
    // instance fields
    int area;

    // constructor method
    public Calc(int one, int two, int three) {
        area = one * two * three;
    }

    // main method
    public static void main(String[] args) {
        int sideOne = 2;
        int sideTwo = 3;
        int sideThree = 1;

        Calc mult = new Calc(sideOne, sideTwo, sideThree);

        System.out.println(mult.area);
    }
}

暂无
暂无

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

相关问题 为什么在将数组传递给方法时得到找不到符号 - why am I getting the cannot find symbol when passing an array into a method 此方法的参数有什么问题/为什么会出现此错误? - What is wrong with the parameters of this method/Why am I getting this error? 在没有最终参数声明的情况下将最终参数传递给另一个方法 - Passing final parameters to another method without final parameter declaration “无效的方法声明; 尝试调用方法时返回“需要返回类型” - “invalid method declaration; return type required” when trying to call a method Java中的“无效的声明方法” - “Invalid declaration method” in java 尽管传递了预期的参数,为什么我仍然面临 junit 5 assertThrows 断言 TimedOutException 的问题? - Why am I facing issue with junit 5 assertThrows for asserting TimedOutException despite passing expected parameters? 我似乎无法找出为什么我不断收到错误“无效的方法声明;需要返回类型” - I can't seem to find out why I keep getting the error "Invalid method declaration; return type required" 为什么即使使用hasNextInt()方法,我也会得到inputMismatchException? - Why am I getting inputMismatchException even when using hasNextInt() method? 为什么调用带有回调的方法时没有得到任何输出? - why am I not getting any output when a method with a callback is called? 为什么在This上调用方法时会出现NullPointerException? - Why am I getting a NullPointerException when invoking a method on This?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM