App下載

Java面向?qū)ο髮?shí)現(xiàn)簡(jiǎn)單的猜拳小游戲 附詳細(xì)代碼

猿友 2021-07-30 12:01:03 瀏覽數(shù) (2619)
反饋

本篇文章將運(yùn)用Java面向?qū)ο蟮幕局R(shí),來實(shí)現(xiàn)一個(gè)簡(jiǎn)單的猜拳小游戲,以下是詳情內(nèi)容,以供大家學(xué)習(xí)參考。

1 要求

與電腦進(jìn)行猜拳并記錄分?jǐn)?shù)。

2 Computer.java 源代碼(電腦自動(dòng)隨機(jī)出拳)

public class Computer {
    public void Fingers(int index) {
        String[] fingers = {"石頭", "剪刀", "布"};
        System.out.println("電腦出拳:" + fingers[index]);
    }
}

3 Game.java 源代碼(實(shí)現(xiàn)主要功能的類)

import java.util.Random;
import java.util.Scanner;

public class Game {
    public void initial(){
        //頁面
        Scanner scanner = new Scanner(System.in);
        System.out.println("-------------------歡迎進(jìn)入游戲界面-------------------");
        System.out.println();
        System.out.println("               ***********************");
        System.out.println("               *******猜拳,開始!******");
        System.out.println("               ***********************");
        System.out.println("出拳規(guī)則:0.石頭 1.剪刀 2.布");

        //輸入姓名
        System.out.print("請(qǐng)輸入你的姓名:");
        String name = scanner.next();
        System.out.println(name+" VS 汶老板  對(duì)戰(zhàn)");

        //判斷是否開始
        System.out.print("要開始嗎?(輸入yes/no):");
        String b =scanner.next();
        int count = 0;
        int usercount=0;
        int computercount=0;
        while (b.equals("yes")){
            //統(tǒng)計(jì)對(duì)戰(zhàn)次數(shù)
            count++;

            //玩家出拳
            System.out.print("請(qǐng)出拳 0.石頭 1.剪刀 2.布(輸入相對(duì)應(yīng)的數(shù)字):");
            int a = scanner.nextInt();
            String [] fingers = {"石頭","剪刀","布"};
            System.out.println("你出拳:"+fingers[a]);

            //電腦隨機(jī)出拳
            Random random = new Random();
            int index = random.nextInt(3);
            Computer computer = new Computer();
            computer.Fingers(index);

            //判斷輸贏
            if ((a == 0 && index == 1)||(a == 1 && index == 2)||(a == 2 && index == 0)) {
                System.out.println("結(jié)果說:恭喜你,你贏得了!");
                usercount = usercount+1;
            }else if ((a == 0 && index == 2)||(a == 1 && index == 0)||(a == 2 && index == 1)){
                System.out.println("結(jié)果說:很抱歉,你輸了!");
                computercount = computercount+1;
            }else{
                System.out.println("結(jié)果說:平局!");
            }

            //是否進(jìn)行下一局
            System.out.print("是否繼續(xù)進(jìn)行下一局!(輸入yes/no)");
            b = scanner.next();
        }

        //總結(jié)
        System.out.println("----------------------總結(jié)---------------------");
        System.out.println(name+" VS 汶老板");
        System.out.println("對(duì)戰(zhàn)次數(shù):"+count);
        System.out.println("姓名		得分");
        System.out.println("汶老板	"+computercount);
        System.out.println(name+"	"+usercount);
        String i = computercount>usercount?"很遺憾,你輸了本場(chǎng)比賽!":computercount<usercount?"恭喜你,你贏得了本場(chǎng)比賽!":"平局!";
        System.out.println(i);
    }
}

3 Client.java 源代碼(測(cè)試類)

public class Client {
    public static void main(String[] args) {
        Game game  = new Game();
        game.initial();
    }
}

4 結(jié)果截圖

202151995346555

以上就是關(guān)于運(yùn)用 Java 面向?qū)ο蟮幕局R(shí)實(shí)現(xiàn)猜拳小游戲的全部?jī)?nèi)容,想要了解更多有趣好玩的 Java 相關(guān)小游戲的內(nèi)容,可以搜索W3Cschool其他相關(guān)文章內(nèi)容,也希望大家多多關(guān)注和支持我們!


0 人點(diǎn)贊