猜拳游戏

2022-07-31,

第一次写博客,还不熟练;
猜拳游戏是这几天要做的任务,
我做的这个相对比较简单易懂,只涉及到循环,判断,输入.
没有使用接口,继承,

package com.gxc.tow05;
import java.util.Scanner;
public class MareGame {
    public static void main(String[] args) {
        while (true) {
            System.out.println("*******************************");
            System.out.println("--------猜拳游戏开始--------");
            System.out.println("请出拳:(1是剪刀,2是石头,3是布, 4是退出)");
            Scanner sc=new Scanner(System.in);
            int person=sc.nextInt(); //获取用户输入
            //判断输入的值是否准守规则

            while (true){
                if(person>4||person<1){
                    System.out.println("输入有误,请重新输入.");
                    sc=new Scanner(System.in);
                    person=sc.nextInt();
                }else {
                    break;
                }
            }
            int computer=(int)(Math.random()*3)+1; //电脑随机出拳
            String per="用户";
            String com = "电脑";
            //用户出拳
            switch(person){
                case 1:
                    per="剪刀";
                    break;
                case 2:
                    per="石头";
                    break;
                case 3:
                    per="布";
                    break;
            }
            //电脑出拳
            switch(computer){
                case 1:
                    com="剪刀";
                    break;
                case 2:
                    com="石头";
                    break;
                case 3:
                    com="布";
                    break;
            }

            //根据出拳判断输赢
            if(person==1&&computer==3||person==2&&computer==1||person==3&&computer==2){
                System.out.println("你出的是("+per+") 电脑出的是("+com+")");
                System.out.println("       【你赢了!再来一次吧.退出请输入:4】");
                //System.out.println();
            }else if (person==computer){
                System.out.println("你出的是("+per+") 电脑出的是("+com+")");
                System.out.println("       【平局!再来一次吧.退出请输入:4】");
                // System.out.println();
            }else if (person==4){
                System.out.println("【欢迎下次再来,游戏结束!】");;
                break;
            }
            else{
                System.out.println("你出的是("+per+") 电脑出的是("+com+")");
                System.out.println("       【你输了!!!,加油】");
                System.out.println("再来一局吧,退出请输入:4");
            }
        }
    }
}


本文地址:https://blog.csdn.net/qq_45057905/article/details/107864780

《猜拳游戏.doc》

下载本文的Word格式文档,以方便收藏与打印。