stringRounds 正在循环,拒绝有效值

stringRounds is Looping, Rejecting Valid Value

当我输入无效值时循环正常,但当我输入有效值时它仍然显示相同的消息。请帮忙。

public class RockPaperScissors {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random rnd = new Random();
    
        String stringRounds = " ";
    
        System.out.println("Welcome to Rock, Paper Scissors!");
        System.out.println("Let's begin with the number of rounds you would like to play: " );
        stringRounds = sc.nextLine();  
        
        int rounds = Integer.parseInt(stringRounds);
        
        while (rounds < 1 || rounds > 10) {
            System.out.println(stringRounds + (" is out of my range. Please try again."));
            stringRounds = sc.nextLine();
        }
        System.out.println(stringRounds +(" sounds good to me. Let's Get Started!!"));
    }
}

因为你没有在 while 循环中更新 rounds 的值。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random rnd = new Random();
    
        String stringRounds = " ";
    
        System.out.println("Welcome to Rock, Paper Scissors!");
        System.out.println("Let's begin with the number of rounds you would like to play: " );
        stringRounds = sc.nextLine();  
        
        int rounds = Integer.parseInt(stringRounds);
        
        while (rounds < 1 || rounds > 10) {
            System.out.println(stringRounds + (" is out of my range. Please try again."));
            stringRounds = sc.nextLine();
            rounds=  Integer.parseInt(stringRounds);//add this row
        }
        System.out.println(stringRounds +(" sounds good to me. Let's Get Started!!"));
    }

您在 while 循环中对 rounds 进行了条件设置,但未修改其值。

此外,您应该在需要时声明您的 new Random(),我建议您改用 Random.nextInt(n),它的预测性较差。

最后一件事,为什么要使用字符串供用户选择?您需要解析它...您应该改用 int。