• 第十三章 人机猜拳事例


    public class Computer {
    String name = "电脑";// 名字
    int score = 0;// 积分
    public int showFist() {
    //产生随机数
    int show = (int) ((Math.random()*10)%3+1); // 产生随机数,表示电脑出拳
    //输出出拳结果并返回
    switch (show) {
    case 1:
    System.out.println(name+"出拳:剪刀");
    break;
    case 2:
    System.out.println(name+"出拳:石头");
    break;
    case 3:
    System.out.println(name+"出拳:布");
    break;
    }

    return show;
    }

    public class Person {
    String name = "匿名"; //名字
    int score = 0; //积分

    public int showFist(){
    //接收用户的选择
    Scanner input = new Scanner(System.in);
    System.out.print(" 请出拳(1.剪刀2.石头3.布):");
    int show = input.nextInt();
    //输出出拳结果,并且返回
    switch (show) {
    case 1:
    System.out.println("你出拳:剪刀");
    break;
    case 2:
    System.out.println("你出拳:石头");
    break;
    case 3:
    System.out.println("你出拳:布");
    break;
    }
    return show;

    }

    import java.util.Scanner;

    public class Game {
    Person person ; //用户

    Computer computer;//人机
    int count;// 对战次数

    public void inital(){
    person = new Person();
    computer = new Computer();
    count = 0;
    }
    //开始游戏
    public void startGame(){
    System.out.println("-------------欢迎进入猜拳游戏---------------");
    System.out.println();
    System.out.println("******************************************");
    System.out.println(" ** 猜拳,开始 **");
    System.out.println("******************************************");
    System.out.println();
    System.out.println("出拳规则:1.剪刀2.石头3.布");
    Scanner input = new Scanner(System.in);
    String exit="n";//退出系统
    do {
    inital();//初始化
    //选择对手
    System.out.print("选择你的对手(1.刘备2.孙权3.曹操):");
    int role = input.nextInt();
    switch (role) {
    case 1:
    computer.name="刘备";
    System.out.println("你选择了"+ computer.name+"对战");
    break;
    case 2:
    computer.name="孙权";
    System.out.println("你选择了"+computer.name+"对战");
    break;
    case 3:
    System.out.println("你选择了"+computer.name+"对战");
    break;
    default:
    System.out.println("输入错误!");
    break;
    }

    //游戏玩家输入姓名
    System.out.print("请输入你的姓名:");
    person.name=input.next();
    System.out.println(person.name+" VS "+computer.name+"对战 ");
    //开始游戏
    System.out.print("要开始吗?(y/n)");
    //开始一局游戏
    String start = input.next();
    int perFist;//用户出拳
    int comFist;//电脑出拳
    while (start.equals("y")) {
    //出拳
    perFist = person.showFist();
    comFist = computer.showFist();
    //判断胜负
    if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)) {
    System.out.println("结果:和局,真衰! ");//平局
    }else if ((perFist==2&&comFist==1)||(perFist==3&&comFist==2)||(perFist==1&&comFist==3)) {
    System.out.println("结果说:^_^,你输了,真笨! ");//计算机赢
    computer.score++;
    }else {
    System.out.println("结果:恭喜,你赢了! ");//胜利
    person.score++;
    }
    count++;
    System.out.print("是否进行下一轮(y/n)");
    start = input.next();
    }
    //显示结果
    showResult();
    System.out.print(" 要开始下一局吗?(y/n):");
    exit = input.next();
    System.out.println();

    } while (!exit.equals("n"));

    System.out.println("系统退出");
    }
    private void showResult() {
    //显示对战次数
    System.out.println("------------------------------");
    System.out.println(computer.name+" VS "+person.name);
    System.out.println("对战次数:"+count);
    //显示最终得分
    System.out.println(" 姓名 得分");
    System.out.println(person.name+" "+person.score);
    System.out.println(computer.name+" "+computer.score+" ");

    //显示对战结果
    int result = 3;
    if (result==1) {
    System.out.println("结果:打成平手,真衰!");
    }else if (result==2) {
    System.out.println("结果:恭喜,你赢了!");
    }else
    {
    System.out.println("结果:呵呵,笨笨,下次加油啊!");//计算机赢
    }
    System.out.println("--------------------------");
    }
    //计算比赛结果
    private int calcResult() {
    // TODO Auto-generated method stub
    if (person.score == computer.score) {
    return 1;//战平
    }else if (person.score > computer.score) {
    return 2;//游戏玩家赢
    }else {
    return 3;//惜败
    }
    }


    }

    public class Test {
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Game game = new Game();

    game.inital();

    game.startGame();
    }

    }

    }


    }

  • 相关阅读:
    jquery获取父元素或父节点的方法
    JS省份联级下拉框
    全国各省、市名称(包括县级市)
    让Vs2010支持 Css3+HTML5
    Sql Server 事务/回滚
    Windows.Forms Panel 动态加载用户控件 UserControl
    C/C++ 运算符 & | 运算
    WPF
    SQL Server 数据库定时自动备份【转】
    如何编写更棒的代码:11个核心要点
  • 原文地址:https://www.cnblogs.com/ringqq/p/9786141.html
Copyright © 2020-2023  润新知