我无法调用方法"加"。我试图添加更多花括号,也是正常花括号,但没有任何帮助!
package example;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
Scanner a = new Scanner(System.in);
Scanner b = new Scanner(System.in);
int varTwo = b.nextInt();
int varOne = a.nextInt();
public static void plus (int aa, int bb) {
return aa+bb;
}
plus(varOne, varTwo);
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
void is an invalid type for the variable plus
Syntax error on token "(", ; expected
Syntax error on token ",", ; expected
Syntax error on token ")", ; expected
Void methods cannot return a value
The method plus(int, int) is undefined for the type HelloWorld
at example.HelloWorld.main(HelloWorld.java:10)
答案 0 :(得分:2)
试试这样:
.center { margin: 0 auto; }
答案 1 :(得分:1)
你不能同时拥有void方法和return语句。如果你想从方法中返回一些东西,请删除void。返回后的代码也无法访问,应该在return语句之前。下面是您可以继续开发的固定代码。
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
Scanner a = new Scanner(System.in);
Scanner b = new Scanner(System.in);
int varTwo = b.nextInt();
int varOne = a.nextInt();
int result = plus(varOne, varTwo);
}
public static int plus (int aa, int bb) {
return aa+bb;
}
}
答案 2 :(得分:0)
您正在方法中定义一个方法,尝试在main之外声明plus
(并删除另一个Scanner
,因为您不需要Scanner
s),e ,G:
public static void main(String[] args){
Scanner a = new Scanner(System.in);
int varOne = a.nextInt();
int varTwo = a.nextInt();
int sum = plus(varOne, varTwo);
System.out.println(sum);
a.close();
}
public static int plus (int aa, int bb) {
return aa+bb;
}
或者更好的是,你甚至不需要一个方法来执行一个简单的操作(除非指定),你可以在main
本身执行它(同样,你不需要两个{{1}的实例还有),例如:
Scanner