幸运猜拳(类和对象的思想)

//user.java
package morra;

import java.util.Scanner;

public class user {
    public static String name;
    public static int go;
    public static int showFist(){
        System.out.println("请选择出拳:1.石头 2.剪刀 3.布");
        Scanner input=new Scanner(System.in);
        int play=input.nextInt();
        if(play==1){
            System.out.println("你出拳: 石头");
        }
        if(play==2){
            System.out.println("你出拳: 剪刀");
        }
        if(play==3){
            System.out.println("你出拳: 布");
        }
        go=play;
        return play;
    }
}

//computer.java
package morra;

public class computer {
    public static String npc;
    public static int random;
    public static int showFist() {
        do {
            random = (int) (Math.random() * 2 + 1);
        } while (random!=1 && random!=2 && random!=3);
        if (random == 1) {
            System.out.println(computer.npc+"出拳: 石头");
        }
        if (random == 2) {
            System.out.println(computer.npc+"出拳: 剪刀");
        }
        if (random == 3) {
            System.out.println(computer.npc+"出拳: 布");
        }
        return random;
    }
    }


Game.java
package morra;

public class Game {
    public static int k;
    public static int j;
    public static void startGame()
    {
        user.showFist();
        computer.showFist();
        k=computer.random;
        j=user.go;
    }
}

//enter.java
package morra;

import java.util.Scanner;

public class enter {
    public static void main(String[] args) {
        System.out.println("*************************");
        System.out.println("****欢迎来到猜拳游戏!****");
        System.out.println("*************************");
        System.out.println("是否开始?y/n");
        Scanner input = new Scanner(System.in);
        String p = input.next();
        while("y".equals(p)) {
            int q = 0, s = 0, t = 0;
            String x;
            System.out.println("请输入您的姓名:");
            String l = input.next();
            user.name = l;
            System.out.println("请选择对战角色:1.刘备 2.曹操 3.孙权");
            int js=input.nextInt();
            System.out.println("选择成功!");
            if(js==1){
                computer.npc="刘备";
            }
            if(js==2){
                computer.npc="曹操";
            }
            if(js==3){
                computer.npc="孙权";
            }
            do {
                Game.startGame();
                if (Game.k == Game.j ) {
                    System.out.println("平局");
                    q++;
                }
                if ((Game.k == 1 && Game.j == 2) || (Game.k == 2 && Game.j == 3) || (Game.k == 3 && Game.j == 1)){
                    System.out.println("很遗憾,您输了");
                    s++;
                }
                if ((Game.k == 2 && Game.j == 1) || (Game.k == 3 && Game.j == 2) || (Game.k == 1 && Game.j == 3)){
                    System.out.println("恭喜您!胜利!");
                    t++;
                }
                System.out.println("是否继续?y/n");
                x = input.next();
            } while ("y".equals(x));
            System.out.println("一共对局了:" + (q + s + t) + "盘");
            System.out.println("胜利"+ t + "盘");
            System.out.println("失败"+ s +"盘");
            System.out.println("平局"+ q +"盘");
            System.out.println("积分表:");
            System.out.println(""+user.name+" "+t);
            System.out.println(""+computer.npc+" "+s);
            System.out.println("重开一局?y/n");
            p=input.next();
        }
        System.out.println("感谢您的游玩!");
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值