Java面向对象之猜拳游戏

Java面向对象之猜拳游戏,供大家参考,具体内容如下

1 要求

与电脑进行猜拳并记录分数。

2 Computer.java 源代码(电脑自动随机出拳)

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

3 Game.java 源代码(实现主要功能的类)

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

public class Game {
    public void initial(){
        //页面
        Scanner scanner = new Scanner(System.in);
        System.out.println("-------------------欢迎进入游戏界面-------------------");
        System.out.println();
        System.out.println("               ***********************");
        System.out.println("               *******猜拳,开始!******");
        System.out.println("               ***********************");
        System.out.println("出拳规则:0.石头 1.剪刀 2.布");

        //输入姓名
        System.out.print("请输入你的姓名:");
        String name = scanner.next();
        System.out.println(name+" VS 汶老板  对战");

        //判断是否开始
        System.out.print("要开始吗?(输入yes/no):");
        String b =scanner.next();
        int count = 0;
        int usercount=0;
        int computercount=0;
        while (b.equals("yes")){
            //统计对战次数
            count++;

            //玩家出拳
            System.out.print("请出拳 0.石头 1.剪刀 2.布(输入相对应的数字):");
            int a = scanner.nextInt();
            String [] fingers = {"石头","剪刀","布"};
            System.out.println("你出拳:"+fingers[a]);

            //电脑随机出拳
            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("结果说:恭喜你,你赢得了!");
                usercount = usercount+1;
            }else if ((a == 0 && index == 2)||(a == 1 && index == 0)||(a == 2 && index == 1)){
                System.out.println("结果说:很抱歉,你输了!");
                computercount = computercount+1;
            }else{
                System.out.println("结果说:平局!");
            }

            //是否进行下一局
            System.out.print("是否继续进行下一局!(输入yes/no)");
            b = scanner.next();
        }

        //总结
        System.out.println("----------------------总结---------------------");
        System.out.println(name+" VS 汶老板");
        System.out.println("对战次数:"+count);
        System.out.println("姓名\t\t得分");
        System.out.println("汶老板\t"+computercount);
        System.out.println(name+"\t"+usercount);
        String i = computercount>usercount?"很遗憾,你输了本场比赛!":computercount 
 

3 Client.java 源代码(测试类)

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

4 结果截图

Java面向对象之猜拳游戏_第1张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Java面向对象之猜拳游戏)