人机猜拳代码

练习用的仿代码 自己堆出来的;

首先堆一个玩家的属性和相应方法:

package csq;

import java.util.Scanner;

public class Person {
    String name;
    int score;
    public int showFist(){
        System.out.println("请出拳:1.剪刀 2.石头 3.布");
        Scanner iuput=new Scanner(System.in);
        int num=iuput.nextInt();
        String fist="";
        switch (num){
            case 1:
                fist="剪刀";
                break;
            case  2:
                fist="石头";
                break;
            case 3:
                fist="布";
                break;
        }
        System.out.println(name+"的出拳为:"+fist);
        return num;
    }
}

再创建一个computer的属性和猜拳方法:

package csq;

public class Computer {
    String name;
    int score;
    public int Showfist(){
        int num=(int)(Math.random()*3)+1;
        String fist="";
        switch (num){
            case 1:
                fist="剪刀";
                break;
            case 2:
                fist="石头";
                break;
            case 3:
                fist="布";
                break;
        }
        System.out.println(name+"出拳为:"+fist);
        return num;
        }
    }

然后是游戏界面包含引用:

package csq;

import java.util.Scanner;

public class Game {
    int count;
    Person person=new Person();
    Computer computer=new Computer();

public int initia(){
    count=0;
    return count;
}
public void startGame(){
    System.out.println("---------欢迎进入游戏世界---------\n");
    System.out.println("\t\t************");
    System.out.println("\t\t**   😊猜拳,start☺   **");
    System.out.println("\t\t************");
    System.out.println("\n\n 出拳规则:1.剪刀 2.石头 3.布");
    Scanner iuput=new Scanner(System.in);
    System.out.println("请选择对手:1. 刘备 2.关羽 3.张飞");
    int num=iuput.nextInt();
    switch (num){
        case 1:
            computer.name="刘备";
            break;
        case 2:
            computer.name="关羽";
            break;
        case 3:
            computer.name="张飞";
            break;
    }
    System.out.print("请输入你的名字:");
    person.name=iuput.next();
    System.out.println(person.name+"VS"+computer.name+"对战\n");
    System.out.print("\n要开始了么?(y/n)");
    String con=iuput.next();
    int perFist;
    int comFist;
    if (con.equals("y")){
        String answer="y";
    while ("y".equals(answer)){
        perFist=person.showFist();
        comFist=computer.Showfist();
        if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)){
            System.out.println("平局\n");
        }
        else if ((perFist==3)&&(comFist==2)||(perFist==2)&&(comFist==1)||(perFist==1)&&(comFist==3)){
            System.out.println("恭喜你,你赢了");
            person.score++;
        }
        else {
            System.out.println("你输了");
            person.score++;
    }
        count++;
        System.out.println("\n是否开始下一轮(y/n)");
        answer=iuput.next();
}
}
}
public void showRosult(){
    System.out.println("---------------------------");
    System.out.println(computer.name+"VS"+person.name);
    System.out.println("对战次数:"+count);
    System.out.println("\n 姓名\t得分\n"+person.name+"\t\t"+person.score+"\n"+computer.name+"\t\t"+computer.score+"\n");
    if (computer.score==person.score){
        System.out.println("平局");
    }else if (computer.score>person.score){
        System.out.println("你输了");
    }else {
        System.out.println("你赢了");
    }
    System.out.println("-------------------------");
}
}

这玩意很烦对于初学者无框架的人来说,写出来要用到之前学的东西,之前是不记笔记的,但是后来的初学者特别是自学者最好养成记笔记的习惯。

最后一个测试类:

代码如下:

package csq;

public class Text {
    public static void main(String[] args) {
        Game start=new Game();
        start.initia();
        start.startGame();
        start.showRosult();
    }
}

这个就是main方法加上调用作用,没啥难的。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值