要求:
我做了一个逻辑但是无法检查条件
public static void main(String args[]) throws Exception {
boolean quit = false;
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number :");
int n;
while (!quit) {
if (sc.hasNextInt()) {
n = sc.nextInt();
while (n <= 0) {
System.out.println("*****Fibonacci Series*****");
int f1, f2 = 0, f3 = 1;
for (int i = 1; i <= n; i++) {
f1 = f2;
f2 = f3;
f3 = f1 + f2;
System.out.print(" " + f3 + " ");
}
}
quit = false;
} else if (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next();
quit = false;
} else(sc.hasNextInt().equals("q")) {
quit = true;
}
}
}
如果条件它抛出错误有.class预期
答案 0 :(得分:0)
您要查找的if条件是if (sc.hasNextInt())
之后,您可以致电sc.nextInt();
整个检查可能如下所示:
if (sc.hasNextInt()) {
int num = sc.nextInt();
//do something with num
}
else if (sc.hasNext() && ("q".equals(sc.next())) ) {
//do something
}
有关Scanner
的详细信息,请阅读JavaDoc。