public static void main(String[] args) {
Scanner play = new Scanner(System.in);
System.out.println("Enter price of shoe 1: ");
int n = play.nextInt();
System.out.println("Enter price of shoe 2: ");
int m = play.nextInt();
calculateShoe(int n, int m);
}
public static int calculateShoe(int n, int m) {
if(n < m) {
System.out.println("Shoe 1 is less than shoe 2.");
}
else {
System.out.println("Your total is: " + n + " and your savings are: " + m);
}
}
}
我在这个程序中得到的唯一错误是回调递归方法。我不知道如何使用用户输入回调程序。
-
您不应该调用
calculateShoe(int n, int m)
。应该是calculateShoe(n, m);
-
并且
calculateShoe
的返回类型应该是void
而不是int
。 -
关闭扫描器对象
play.close();
你贴出来的代码和递归一点关系都没有
你可以试试下面的代码:
import java.util.Scanner;
public class RecursionDemo
{
public static void main(String[] args) {
Scanner play = new Scanner(System.in);
System.out.println("Enter price of shoe 1: ");
int n = play.nextInt();
System.out.println("Enter price of shoe 2: ");
int m = play.nextInt();
int choice;
calculateShoe(n, m);
do {
System.out.print("Press 1 for exit : ");
choice = play.nextInt();
if(choice > 0 && choice != 1) {
System.out.println("Enter price of shoe 1: ");
int n1 = play.nextInt();
System.out.println("Enter price of shoe 2: ");
int m1 = play.nextInt();
calculateShoe(n1, m1);
}
} while(choice!=1);
System.out.println();
System.out.println("You are exited from program...");
}
public static int calculateShoe(int n, int m) {
if(n < m) {
System.out.println("Shoe 1 is less than shoe 2.");
}
else {
System.out.println("Your total is: " + n + " and your savings are: " + m);
}
return 0;
}
}