如何在递归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);
}


}

}

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

  1. 您不应该调用calculateShoe(int n, int m)。应该是calculateShoe(n, m);

  2. 并且calculateShoe的返回类型应该是void而不是int

  3. 关闭扫描器对象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;
}
}

相关内容

  • 没有找到相关文章

最新更新