【发布时间】:2016-11-15 00:19:55
【问题描述】:
我很困惑,我想使用递归从用户那里得到一个正整数,用户可以输入单词,也可以输入数字。(单词和负数是无效的),不知道我做错了什么。 很抱歉忘了问这个问题,我遇到编译器错误,当我尝试编译它时,我的扫描仪有问题,我正在阅读它说我必须为用户输入使用参数,我'不知道怎么做,第二个问题是,如果用户输入 n
import java.util.*;
public class Recursion {
private static int getPositiveInt(Scanner keyboard) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a positive interger, n");
int n = keyboard.nextInt();
if (n > 0) {
return n;
}
else { //here the code should rerun if invalid input, but I cant figure it out
System.out.println("enter a positive number");
}
}
}
【问题讨论】:
标签: recursion