提问人:Francis Putnam 提问时间:10/20/2022 更新时间:10/20/2022 访问量:28
nextInt 行后面的语句不起作用
Statements after the nextInt Line is not working
问:
我希望用户根据他输入的水果数量输入要捕获的水果的选择。但问题是,在 nextInt() 上输入后,编译器停止编译并跳过 for 循环。
import java.util.*;
public class FruitBasket {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Stack<String> stack = new Stack<>();
String select;
System.out.println("Catch and eat any of these fruits: (Apple, Orange, Mango, Guava");
System.out.print("How many fruits would you like to catch? ");
int num = s.nextInt();
s.nextLine();
for (int i = 1; i >= num; i++) {
System.out.print("Choose a fruit to catch. Press A(apple), O(orange), M(mango), G(guava) ");
System.out.print("\nFruit " + i + " of " + num + ": ");
select = s.nextLine();
stack.push(select);
}
}
}
答: 暂无答案
评论
for (int i = 1; i >= num; i++)
- 除非 num 为 1 或更小