You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

剪刀石头布游戏循环中gameTie布尔值未重置导致无限循环求助

剪刀石头布游戏无限循环问题解决

问题根源

  1. gameTie变量未正确更新:main方法中,gameTie一旦被设为true(首次平局后),后续迭代中没有在分出胜负时将其重置为false,导致循环永远满足while(gameTie == true)的条件,陷入无限循环。
  2. 冗余且错误的逻辑判断:你已经在method4中计算并返回了是否平局的结果,但main方法里又重复判断了一次CPUchoice == userChoiceInt,既冗余又没利用method4的返回值来更新gameTie。
  3. CPU随机数生成错误:method1中的rand.nextInt(2)只能生成0和1,无法覆盖剪刀(对应2)的情况,应该改为rand.nextInt(3)。

修复方案

  1. 直接使用method4的返回值来更新main方法中的gameTie变量,确保每次迭代都能获取最新的游戏状态。
  2. 删除main方法中自己判断平局的冗余代码。
  3. 修正method1中的随机数生成逻辑。

修改后的完整代码

import java.util.Random;
import java.util.Scanner;
public class Lab9_2 {
    public static void main(String[] args) {
        System.out.println("This program plays Rock-Paper-Scissors against the computer.\n"
                         + "When there is a tie, the game will restart until a winner is chosen.");
        boolean gameTie;
        do {
            int CPUchoice = method1();
            //generate the cpu's play
            
            int userChoiceInt = method2();
            //get the user's play
            
            method3(CPUchoice);
            //output cpu's play
            
            // 直接用method4的返回值更新gameTie
            gameTie = method4(CPUchoice, userChoiceInt);
            
        }while(gameTie); // 简化条件判断,等价于gameTie == true
        
    }
    
    public static int method1() {
        //generate random num 0-2, represents CPU's play
        Random rand = new Random();
        int CPUchoice = rand.nextInt(3); // 修正为3,生成0、1、2
        return CPUchoice;
    }
    
    public static int method2() {
        //get user's play
        //includes input checking for error prevention
        Scanner kb = new Scanner(System.in);
        boolean uInput; //safe input marker
        char userInputChar; //the character enetered by user for their play
        int userChoiceInt = 0; //the users play, in integer
        
            do { //run user input until user enters a safe input
                System.out.println("\n\nPlease input your choice:");
                userInputChar = kb.next().charAt(0); //get the char input from user
            
                    if(userInputChar == Character.toLowerCase('r')) { 
                        //rock 'r' = 0
                        userChoiceInt = 0;
                        uInput = true;
                    }else if(userInputChar == Character.toLowerCase('p')) { 
                        //paper 'p' = 1
                        userChoiceInt = 1;
                        uInput = true;
                    }else if(userInputChar == Character.toLowerCase('s')) { 
                        //scissors 's' = 2
                        userChoiceInt = 2;
                        uInput = true;
                    }else {
                        //Output error message. mark that user's input is nonsafe
                        System.out.println("Sorry, that's not a valid play. Please try again.");
                        uInput = false;
                    }
            }while (!uInput); // 简化条件判断
            
        //return the int value of user's input
        return userChoiceInt; 
    }
    
    public static void method3(int CPUchoice) {
        //output the play made by the cpu in method1()
        String CPUchoiceStr = "";
        if (CPUchoice == 0) {
            CPUchoiceStr = "rock.";
        }
        else if (CPUchoice ==1) {
            CPUchoiceStr = "paper.";
        }
        else if (CPUchoice == 2) {
            CPUchoiceStr = "scissors.";
        }
        System.out.println("The CPU played "+CPUchoiceStr);
    }
    
    public static boolean method4(int CPUchoice, int userChoiceInt) {
        //get CPU choice and user choice
        //calculate and output winner
        boolean gameTie = false;
            if (CPUchoice == userChoiceInt) {
                //tie
                System.out.println("It's a tie!");
                gameTie = true;
            }
            else if((CPUchoice == 0) && (userChoiceInt == 1)) {
                //cpu = rock user = paper
                System.out.println("Paper covers rock. You win!");
            }
            else if((CPUchoice == 0)&&(userChoiceInt ==2)) {
                //cpu = rock user = scissors
                System.out.println("Rock breaks scissors. You lose.");
            }
            else if((CPUchoice == 1)&&(userChoiceInt == 0)) {
                //cpu = paper user = rock
                System.out.println("Paper covers rock. You lose.");
            }
            else if((CPUchoice == 1)&&(userChoiceInt == 2)) {
                //cpu = paper user = scissors
                System.out.println("Scissors cuts paper. You win!");
            }
            else if((CPUchoice == 2)&&(userChoiceInt==0)) {
                //cpu = scissors user = rock
                System.out.println("Rock breaks scissors. You win!");
            }
            else if((CPUchoice == 2)&&(userChoiceInt ==1)) {
                //cpu = scissors user = paper
                System.out.println("Scissors cuts paper. You lose.");
            }
        return gameTie;
    }
}

关键修改点说明

  • main方法中,删除了自己判断平局的代码,改为直接将method4的返回值赋值给gameTie,确保每次迭代都会更新游戏状态。
  • method1中把rand.nextInt(2)改为rand.nextInt(3),让CPU能随机生成石头、布、剪刀三种选择。
  • 简化了部分条件判断(比如while (uInput != true)改为while (!uInput)),让代码更简洁易读。

内容的提问来源于stack exchange,提问作者Kachi03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 23:45:54