簡體   English   中英

剪刀石頭布游戲領帶 boolean 不會在循環內重新初始化

[英]Rock-Paper-Scissors game tie boolean does not re-initialize within a loop

我正在使用四種方法為 class 開發剪刀石頭布程序。如果用戶和 CPU 平局,部分任務是重復游戲,並在有贏家時結束游戲。 我通過在 main() 中通過一個 do-while 循環運行整個程序來完成此操作,只要游戲綁定 (gameTie = true),該循環就會運行。 我遇到的問題是 boolean gameTie 的值僅在第一場比賽后發生變化。 如果第一場比賽是平局,程序將無限循環。 如果有贏家,則游戲結束。 我不確定為什么 boolean gameTie 的值在每次迭代后都沒有被重新初始化(意思是在每次平局或獲勝后)。 任何建議表示贊賞。

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 = false;
        do {
            int CPUchoice = method1();
                //generate the cpu's play
            
            int userChoiceInt = method2();
                //get the user's play
            
            method3(CPUchoice);
                //output cpu's play
            
            method4(CPUchoice, userChoiceInt);
                //compare both user's play. if they are tied, mark gameTie as true to rerun the loop
                if (CPUchoice == userChoiceInt) {
                    gameTie = true;
                }
            
        }while(gameTie == true);
        
    }
    
    public static int method1() {
        //generate random num 1-3, represents CPU's play
        Random rand = new Random();
        int CPUchoice = rand.nextInt(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' = 1
                        userChoiceInt = 0;
                        uInput = true;
                    }else if(userInputChar == Character.toLowerCase('p')) { 
                        //paper 'p' = 2
                        userChoiceInt = 1;
                        uInput = true;
                    }else if(userInputChar == Character.toLowerCase('s')) { 
                        //scissors 's' = 3
                        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 != true);
            
        //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 winnere
        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!");
                gameTie = false;
            }
            else if((CPUchoice == 0)&&(userChoiceInt ==2)) {
                //cpu = rock user = scissors
                System.out.println("Rock breaks scissors. You lose.");
                gameTie = false;
            }
            else if((CPUchoice == 1)&&(userChoiceInt == 0)) {
                //cpu = paper user = rock
                System.out.println("Paper covers rock. You lose.");
                gameTie = false;
            }
            else if((CPUchoice == 1)&&(userChoiceInt == 2)) {
                //cpu = paper user = scissors
                System.out.println("Scissors cuts paper. You win!");
                gameTie = false;
            }
            else if((CPUchoice == 2)&&(userChoiceInt==0)) {
                //cpu = scissors user = rock
                System.out.println("Rock breaks scissors. You win!");
                gameTie = false;
            }
            else if((CPUchoice == 2)&&(userChoiceInt ==1)) {
                //cpu = scissors user = paper
                System.out.println("Scissors cuts paper. You lose.");
                gameTie = false;
            }
        return gameTie;
    }
}

我已經嘗試初始化 boolean gameTie 的外部變量(意味着在任何循環或 if/else 語句之外初始化它們),也讓它們保持未初始化狀態,直到從循環條件初始化。 我期待 boolean gameTie 在游戲的每次迭代后都有一個初始化值,但它只是在第一場比賽后才初始化。 如果第一個游戲結果是平局,那么程序將無限循環游戲,即使有贏家(這意味着應該將 false 初始化為 gameTie 並且 main() 中的 do-while 循環不會再次運行)。 如果第一個游戲有獲勝者,則應將 false 初始化為 gameTie 並且程序應該結束(它確實如此)。

您的 Method4 正在返回 gameTie 值,但您在 Main Method 中沒有對其進行任何操作,您應該說:

gameTie = method4(CPUchoice, userChoiceInt);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM