我似乎找不到这个错误的解决方案..请不要评判我。初学者在这里。
import java.util.Scanner;
public class GreatestCommonDivisor {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int num1;
int num2;
int gcd;
boolean again = false;
do {
System.out.print("First positive integer: ");
num1 = inputPosInt();
System.out.print("Second positive integer: ");
num2 = inputPosInt();
//....
gcd = gcd();
(again) = tryAgain();
} while (again); {
System.out.println("Thank you for testing this program.");
}
}
public static int inputPosInt () {
Scanner kbd = new Scanner(System.in);
int num;
num = kbd.nextInt();
if (num < 0) {
System.out.println("Entered integer value is non-positive.");
return 0;
}
return num;
}
public static int gcd(int num1, int num2) {
if (num2==0) {
return num1;
}
return gcd(num2,num1%num2);
}
public static boolean tryAgain() {
Scanner kbd = new Scanner(System.in);
System.out.print("Would you like to try again? [Y/N]");
char again;
again = kbd.nextLine().charAt(0);
if (again == 'y' || again == 'Y') {
return true;
}
else if (again == 'n' || again == 'N') {
System.exit(0);
}
return false;
}
}
该程序需要 2 个正整数输入,并使用欧几里得算法求解其 GCD。
错误:gcd = gcd(); ^ 必需:int,int 找到:无参数原因:实际参数列表和形式参数列表长度不同 1 错误