Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决Java';。类别';预期错误为Java,编译失败_Java - Fatal编程技术网

如何解决Java';。类别';预期错误为Java,编译失败

如何解决Java';。类别';预期错误为Java,编译失败,java,Java,我试图创建一个java程序,它可以添加2个数字,但不断出现这个错误 error: '.class' expected return int ad(); 1 error error: compilation failed 这是我的密码 public class Sum { int a; int b; int add; public int ad(int a, int b){ int add = (int)

我试图创建一个java程序,它可以添加2个数字,但不断出现这个错误

error: '.class' expected  
  return int ad();              

1 error  
error: compilation failed
这是我的密码

public class Sum {
    int a;
    int b;
    int add;

    public int ad(int a, int b){
        int add = (int) a + b;
        return add;
    }

    public static void main(String[] args) {
        return int ad();
    }

}
  • main()
    的返回类型是
    void
    -它不能返回任何内容。它主要用于调用函数
  • 将参数传递给
    ad()
    ——否则会出现编译时异常——它需要2个整数
  • 这里的冗余强制转换:
    intadd=(int)a+b
    -对于这样一个简单的方法,您可以直接
    返回a+b
  • 未使用的变量-所有成员变量都未使用

  • 解决方案2(使用成员变量):


    代码中有一些要点

    public class Main {
    
    public int add(int a, int b){
        int result = a + b;
        return result;
    }
    
    public static void main(String[] args) {
       Main main = new Main();
       System.out.println(main.add(1, 1));
    }
    
    }
    
  • main()无法返回任何内容,因为它是一个void方法
  • 在这种情况下,您不必使用成员变量。成员变量在类中使用最多。例如,当您想要创建Person类时
  • 您不必键入int add=(int)a+b,因为您不必转换任何内容。数据类型已经是int
  • 下面是一个您可以做的示例

    public class Main {
    
    public int add(int a, int b){
        int result = a + b;
        return result;
    }
    
    public static void main(String[] args) {
       Main main = new Main();
       System.out.println(main.add(1, 1));
    }
    
    }
    
    输出:

    public class Sum {
        int a;
        int b;
        int add;
    
        public int ad(int a, int b) {
            int add = (int) a + b;
            return add;
        }
    
        public static void main(String[] args) {
            Sum sum = new Sum(); // Make an instance of class Sum
            int result = sum.ad(1, 2); // and call method ad
            System.out.println("Result: " + result); // Output: 'Result: 3'
        }
    }
    
    public class Sum {
    
        public static int ad(int a, int b) { // Make method ad static
            int add = (int) a + b;
            return add;
        }
    
        public static void main(String[] args) {
            int result = Sum.ad(1, 3); // Calling static method ad
            System.out.println("Result: " + result); // Output: 'Result: 3'
        }
    }
    
    二,

    希望这些信息有用

    方法“public static void main(String[]args)”是静态的,而方法“public public int ad(int a,int b)”是非静态的。

    如果您想访问方法“publicintad(inta,intb)”,则创建类Sum的实例并调用方法“ad(inta,intb)”,或者将方法“ad(inta,intb)”设置为静态。正如上面的注释中已经提到的,“publicstaticvoidmain(String[]args)”没有返回类型-它是void,所以main方法中不需要“returnintad()”

    本地1-创建类Sum的实例并调用方法ad(int a,int b):

    public class Sum {
        int a;
        int b;
        int add;
    
        public int ad(int a, int b) {
            int add = (int) a + b;
            return add;
        }
    
        public static void main(String[] args) {
            Sum sum = new Sum(); // Make an instance of class Sum
            int result = sum.ad(1, 2); // and call method ad
            System.out.println("Result: " + result); // Output: 'Result: 3'
        }
    }
    
    public class Sum {
    
        public static int ad(int a, int b) { // Make method ad static
            int add = (int) a + b;
            return add;
        }
    
        public static void main(String[] args) {
            int result = Sum.ad(1, 3); // Calling static method ad
            System.out.println("Result: " + result); // Output: 'Result: 3'
        }
    }
    
    备选方案2-使方法ad(int a、int b)静态:

    public class Sum {
        int a;
        int b;
        int add;
    
        public int ad(int a, int b) {
            int add = (int) a + b;
            return add;
        }
    
        public static void main(String[] args) {
            Sum sum = new Sum(); // Make an instance of class Sum
            int result = sum.ad(1, 2); // and call method ad
            System.out.println("Result: " + result); // Output: 'Result: 3'
        }
    }
    
    public class Sum {
    
        public static int ad(int a, int b) { // Make method ad static
            int add = (int) a + b;
            return add;
        }
    
        public static void main(String[] args) {
            int result = Sum.ad(1, 3); // Calling static method ad
            System.out.println("Result: " + result); // Output: 'Result: 3'
        }
    }
    
    请在此处阅读有关静态和非静态方法之间差异的更多信息:
    .

    在返回语句中不需要
    int
    ,它可以是
    return ad()
    ,并且不能从
    void
    方法返回任何值。