Java 猜拳游戲 基礎(chǔ)版本

package homeWork;

import java.util.Scanner;

public class PlayGame2 {
    public static void main(String[] args) {
        
        // 猜拳游戲
        Scanner input = new Scanner(System.in);
        
        //1. 提示游戲 玩法
        System.out.println("歡迎參加 猜拳游戲!!! \n 請(qǐng)選擇(1--3) \n 1.石頭  \n 2.剪刀   \n 3.布\n");
        
        // 新建變量     comBoxing 電腦出拳      playInput 用戶出拳
        int playInput = 0;
        int personScore = 0;
        int computerScore = 0;
        int count = 0;
        // 循環(huán)變量
        String answer = "";
        
        do {
            //2. 接收用戶輸入的數(shù)據(jù)
            boolean flag;
            do {
                // 接收人類出拳
                System.out.print("選擇: ");
                playInput = input.nextInt();
                
                //判斷人類輸入的是否正確
                if(playInput>3 || playInput<0) {
                    System.out.println("輸入選擇錯(cuò)誤 ");
                    flag = false;
                }else {
                    flag = true;
                }
            }while(!flag);
            
            // 判斷人出拳
            switch(playInput) {
            case 1:
                System.out.println("你出拳為: 石頭");
                break;
            case 2:
                System.out.println("你出拳為: 剪刀");
                break;
            case 3:
                System.out.println("你出拳為: 布");
                break;
            }
            //3. 電腦隨機(jī)產(chǎn)生數(shù)字
            int comOutput =  (int)(Math.random()*3)+1;
            // 判段電腦的輸入老確定出拳
            switch(comOutput) {
            case 1:
                System.out.println("電腦出拳為: 石頭");
                break;
            case 2:
                System.out.println("電腦你出拳為: 剪刀");
                break;
            case 3:
                System.out.println("電腦你出拳為: 布");
                break;
            }
            
            // 4.判斷勝負(fù)
            if(comOutput == 1 && playInput == 3 || comOutput == 2 && playInput == 1 || comOutput == 3 && playInput == 2) {
                System.out.println("你贏了");
                personScore++;
            }else if(comOutput == playInput) {
                System.out.println("打平");
            }else{ 
                System.out.println("你輸了");
                computerScore++;
            }
            // 累計(jì)對(duì)戰(zhàn)次數(shù)
            count++;
            System.out.print("\n是否繼續(xù)(y/n) : ");
            answer = input.next();
        }while("y".equals(answer));
        
        //5.統(tǒng)計(jì)勝負(fù)
        System.out.println("\n你   VS 計(jì)算機(jī)");
        System.out.println("對(duì)戰(zhàn)次數(shù): "+ count + "回合");
        System.out.println("雙方積分為: "+personScore + "VS"+ computerScore);
        // 判斷總勝負(fù)
        if(personScore > computerScore) {
            System.out.println("恭喜你贏了本回合");
        }else if(personScore == computerScore) {
            System.out.println("雙方打平");
        }else {
            System.out.println("電腦獲勝");
        }
        // 游戲結(jié)束
        System.out.println("Game Over");
        // 結(jié)束輸入
        input.close();
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容