首页 > 解决方案 > 如何在递归 Java 中使用用户输入

问题描述

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

}

我在这个程序中得到的唯一错误是回调递归方法。我不知道如何使用用户输入调用程序。

标签: javarecursionuser-input

解决方案


  1. 你不应该打电话calculateShoe(int n, int m)。应该只是calculateShoe(n, m);

  2. 同样的返回类型calculateShoe应该是void而不是int

  3. 关闭扫描仪对象play.close();

而且您发布的代码与递归无关。


推荐阅读