nextInt 行后面的语句不起作用

Statements after the nextInt Line is not working

提问人:Francis Putnam 提问时间:10/20/2022 更新时间:10/20/2022 访问量:28

问:

我希望用户根据他输入的水果数量输入要捕获的水果的选择。但问题是,在 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);
    }
}

}

Java for 循环 java.util.scanner

评论

0赞 David Koelle 10/20/2022
你确定 for 循环中的那个大于符号吗?
0赞 Scary Wombat 10/20/2022
for (int i = 1; i >= num; i++) - 除非 num 为 1 或更小
0赞 Scary Wombat 10/20/2022
编译器停止编译 - Lazy Compiler
0赞 Francis Putnam 10/20/2022
谢谢你的回答。只是一个初学者对不起

答: 暂无答案