我收到了几个“错位的构造”错误和“删除令牌”错误。 Oracle网站上对这些错误的描述非常模糊。我无法弄清楚问题是什么,但似乎我有一个不合适的括号抛出整个程序。谁能看到我做错了什么?
public class divisorCalc2 {
import java.util.Scanner; //Imports scanner class
public static int gcd(int num1, int num2) {
if (num2 == 0) {
return num1;
} else {
return gcd(num2, num1 % num2);
}
}
public static void main(String[] args) {
System.out.println("Please enter first integer:");
int firstInt = in.nextInt();
System.out.println("Please enter second integer:");
int secondInt = in.nextInt();
System.out.println(gcd(firstInt,secondInt));
in.close(); //Closes Scanner
}
}
答案 0 :(得分:2)
你不能在另一个方法中使用方法。把整个gcd代码放在main之外。
main (...){
...
}
gcd (...){
...
}
答案 1 :(得分:0)
You have a method inside another, move it outside main method, you don't have a scanner declared too, you imported the scanner class but is not defined yet.
Here:
public static int gcd(int num1, int num2) {
if (num2 == 0) {
return num1;
} else {
return gcd(num2, num1 % num2);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter first integer:");
int firstInt = input.nextInt();
System.out.println("Please enter second integer:");
int secondInt = input.nextInt();
System.out.println(gcd(firstInt, secondInt));
input.close(); //Closes Scanner
}
答案 2 :(得分:0)
In a class definition, package declaration is first line, any import(s) is second line. Next comes the 'public class ...'. Your program fails here itself. Take imports statement above, immediate after package.