码迷,mamicode.com
首页 > 其他好文 > 详细

人机猜拳

时间:2018-10-15 11:47:44      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:加油!   ==   main   work   star   math   this   def   ora   

package com.homework.lhh;

import java.util.Scanner;

public class MoraGame {
    private String playerName;// 玩家姓名
    private String computerName;// 电脑玩家姓名
    public static int playerScore;// 玩家分数
    public static int computerScore;// 电脑分数
    public static int playNumber;// 对战次数

    // 选择对手对战
    @SuppressWarnings("resource")
    public void fight() {
        // TODO Auto-generated method stub
        System.out.print("请选择对方角色(1.刘备   2.孙权   3.曹操):");
        Scanner sc = new Scanner(System.in);
        computerName = sc.next();
        switch (computerName) {
        case "1":
            this.computerName = "刘备";
            break;
        case "2":
            this.computerName = "孙权";
            break;
        case "3":
            this.computerName = "曹操";
            break;
        default:
            System.out.println("你的输入有误!");
            break;
        }
        System.out.print("请输入你的名字:");
        playerName = sc.next();
        System.out.println(playerName + "  VS  " + this.computerName);
    }
    // 开始玩游戏
    @SuppressWarnings("resource")
    public void startPlay() {
        // TODO Auto-generated method stub
        System.out.println();
        System.out.print("要开始吗?(y/n):");
        Scanner sc = new Scanner(System.in);
        String start = sc.next();
        System.out.println("********************************");
        while (start.toLowerCase().equals("y")) {
            System.out.print("请出拳:1.剪刀   2.石头   3.布(输入相应数字):");
            int playerNumber = sc.nextInt();
            System.out.println();
            switch (playerNumber) {
            case 1:
                System.out.println("你出拳:剪刀");
                break;
            case 2:
                System.out.println("你出拳:石头");
                break;
            case 3:
                System.out.println("你出拳:布");
                break;
            default:
                System.out.println("你的输入有误");
                break;
            }
            System.out.println();
            int computerNumber = (int) ((Math.random() * 10) % 3 + 1);// 电脑产生随机数取值在1-3之间
            switch (computerNumber) {
            case 1:
                System.out.print(this.computerName + "出拳:剪刀");
                break;
            case 2:
                System.out.print(this.computerName + "出拳:石头");
                break;
            case 3:
                System.out.print(this.computerName + "出拳:布");
                break;
            }
            System.out.println();
            // 判断对局结果
            if (playerNumber == computerNumber) {

                System.out.println("啊,平局!");
                playNumber++;
            } else if (((playerNumber == 1) && (computerNumber != 2)) || ((playerNumber == 2) && (computerNumber != 3))
                    || ((playerNumber == 3) && (computerNumber != 1))) {

                System.out.println("哇,你赢了,好厉害!");
                playerScore++;
                playNumber++;
            } else {

                System.out.println("^_^!!!你输了,真笨!");
                computerScore++;
                playNumber++;
            }
            System.out.println();
            System.out.print("是否开始下一局(y/n):");
            start = sc.next();
            System.out.println();
            System.out.println("********************************");
        }
        this.gameEnd();
    }
    //游戏结束
    public void gameEnd() {
        // TODO Auto-generated method stub
        System.out.println();
        System.out.println("********************************");
        System.out.println(playerName + "  VS  " + this.computerName);
        System.out.println("对战次数:" + playNumber);
        System.out.println("姓名\t\t得分");
        System.out.println(this.computerName + "\t\t" + computerScore);
        System.out.println(playerName + "\t\t" + playerScore);
        if (playerScore < computerScore) {
            System.out.println("呵呵,笨笨,下次加油!");
        }else if(playerScore == computerScore){
            System.out.println("哇,竟然平局,我们下次一决胜负!");
        }else {
            System.out.println("哇,你好棒啊!");
        }
    }
}

class Ui {
    public void theme() {
        // TODO Auto-generated method stub
        System.out.println("\t\t********************************");
        System.out.println("\t\t**********猜拳,开始*************");
        System.out.println("\t\t********************************");
        System.out.println();
        System.out.println("出拳规则:1.剪刀   2.石头   3.布");
    }
}
package com.homework.lhh;

public class MoraGameTest {
    public static void main(String[] args) {
        MoraGame game = new MoraGame();
        Ui ui = new Ui();
        ui.theme();
        game.fight();
        game.startPlay();
    }
}

 

人机猜拳

标签:加油!   ==   main   work   star   math   this   def   ora   

原文地址:https://www.cnblogs.com/loose/p/9789110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!