如何解决Java中的 ‘.class’ 期望错误,Java编译失败。

huangapple go评论86阅读模式
英文:

How do I solve Java '.class' expected error Java, compilation failed

问题

以下是翻译好的内容:

我试图创建一个Java程序,用于相加两个数字,但一直在遇到这个错误:

错误:期望 '.class'
返回 int ad();

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) {
        return int ad();
    }

}
英文:

I was trying to create a java program that adds 2 numbers but keep getting this error

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

1 error  
error: compilation failed

Here is my code

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();
    }

}

答案1

得分: 1

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

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

    public static void main(String[] args) {
        int sum = ad(1, 3);
        System.out.println(sum);
    }
}
  1. main()的返回类型是void - 它不能返回任何内容。主要用于调用函数。
  2. ad()传递参数 - 否则将会得到编译时异常 - 它需要两个整数。
  3. 这里存在多余的类型转换:int add = (int) a + b; - 对于这样一个简单的方法,你可以直接使用 return a + b;
  4. 未使用的变量 - 所有的成员变量都未被使用。

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

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

    private int ad() {
        return a + b;
    }

    public static void main(String[] args) {
        Sum s = new Sum();
        s.a = 1;
        s.b = 2;
        int sum = s.ad();
        System.out.println(sum);
    }
}
英文:
public class Sum {
    /*
    int a;
    int b;
    int add;
    */

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

    public static void main(String[] args) {
        int sum = ad(1, 3);
        System.out.println(sum);
    }
}
  1. main()'s return type is void - it cannot return anything. Mostly it is used to call functions.
  2. Pass parameters to ad() - or you will get a compile time exception - it is expecting 2 integers.
  3. Redundant casting here: int add = (int) a + b; - for a simple method like this, you can directly return a + b;
  4. Unused variables - all your member variables are unused.

Solution 2 (using member variables):

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

    private int ad() {
        return a + b;
    }

    public static void main(String[] args) {
        Sum s = new Sum();
        s.a = 1;
        s.b = 2;
        int sum = s.ad();
        System.out.println(sum);
    }
}

答案2

得分: 0

你的代码中有几点需要注意

  1. main()方法不能返回任何内容,因为它是一个void方法。
  2. 在这种情况下,你不必使用成员变量。成员变量主要用于类中。比如说,当你想要创建一个Person类时。
  3. 你不需要输入 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));
    }

}

输出

2

希望这些信息对你有所帮助!

英文:

There are some points in your code:

  1. main() can't return anything because it's a void method.
  2. You don't have to use member variables in this situation. Member variables are most used in classes. Like for instance when you want to create a Person class.
  3. You don't have to type int add = (int) a + b because you don't have to convert anything.The datatype is already an int.

Here's an example of what you can do:

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));
}

}

Output:

2

Hope this information was useful!

答案3

得分: 0

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

如果你想调用方法"public int ad(int a, int b)",需要创建一个类Sum的实例并调用方法"ad(int a, int b)",或者将方法"ad(int a, int b)"声明为静态方法。正如上面的注释中已经提到的,"public static void main(String[] args)"没有返回类型 - 它是void类型,因此在主方法中不需要"return int ad()"。

备选方案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(); // 创建Sum类的实例
        int result = sum.ad(1, 2); // 调用ad方法
        System.out.println("Result: " + result); // 输出: 'Result: 3'
    }
}

备选方案2 - 将ad(int a, int b)方法声明为静态:

public class Sum {

    public static int ad(int a, int b) { // 将ad方法声明为静态
        int add = (int) a + b;
        return add;
    }

    public static void main(String[] args) {
        int result = Sum.ad(1, 3); // 调用静态ad方法
        System.out.println("Result: " + result); // 输出: 'Result: 3'
    }
}

了解更多关于静态方法和非静态方法之间区别的内容,请阅读:
https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method

英文:

The method "public static void main(String[] args)" is static and the method "public public int ad(int a, int b)" is non-static.

If you want to reach the method "public int ad(int a, int b)" then make an instance of class Sum and call the method "ad(int a, int b)", or make the method "ad(int a, int b)" static. As already mentioned in comments above, "public static void main(String[] args)" has no return type - it is void, so no "return int ad()" in main method is needed.

Alrernative 1 - Make an instance of class Sum and call method 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'
    }
}

Alternative 2 - Make method ad(int a, int b) static:

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'
    }
}

Read more about diffrence between static and non-static methods here:
https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method#:~:text=A%20static%20method%20belongs%20to%20the%20class%20and%20a%20non,class%20that%20it%20belongs%20to.&text=In%20the%20other%20case%2C%20a,class%20has%20already%20been%20instantiated.

huangapple
  • 本文由 发表于 2020年8月30日 15:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63655164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定