java语言实现《石头剪刀布》游戏

 

作为小白程序猿,今晚在上晚自习的时候突然想到小时候玩的石头剪刀布游戏,于是用java写出来了,比较简单,供大家参考,如有问题恳请大家指正~
package practice;
/** 石头剪刀布游戏 */
import java.util.Scanner;
import java.util.Random;
    public class Demo02 {
        public static void main(String[] args) {
            guessGame();
        }

        public static int getGesture(){
            System.out.println("石头,剪刀,布 !");
            System.out.println("1.石头 2.剪刀 3.布 ,其余数字退出游戏"); 
            Scanner scan = new Scanner(System.in);
            int input = scan.nextInt();
            if(input>3 || input <1){
                return 0;
            }
            return input;
        }

        public static void guessGame() {
            int sumVic = 0;//胜局计数
            int sumLos = 0;//败局计数
            int sumPj = 0;//平局计数
            while (true) {
                int num = getGesture();
                Random rand = new Random();
                int inputIc = rand.nextInt(3) + 1;
                if (num == 0) {//无效值
                    System.out.println("已退出游戏");
                    return;
                }

                if (num == inputIc) {//平局
                    System.out.print("平局!");
                    sumPj ++;
                    switch (num) {
                        case 1:
                            System.out.println("您和电脑出的都是石头!");
                            break;
                        case 2:
                            System.out.println("您和电脑出的都是剪刀!");
                            break;
                        case 3:
                            System.out.println("您和电脑出的都是布!");
                            break;
                    }
                }

                if ((inputIc == 1 && num == 3) || (inputIc == 2 && num == 1) || (inputIc == 3 && num == 2)) {//玩家获胜
                    System.out.print("恭喜您获胜!");
                    sumVic ++;
                    switch (num) {
                        case 1:
                            System.out.println("您出的是石头,电脑出的是剪刀!");
                            break;
                        case 2:
                            System.out.println("您出的是剪刀,电脑出的是布!");
                            break;
                        case 3:
                            System.out.println("您出的是布,电脑出的是石头!");
                            break;
                    }
                }

                if ((inputIc == 3 && num == 1) || (inputIc == 2 && num == 3) || (inputIc == 1 && num == 2)) {
                    System.out.print("很遗憾,您输了!");
                    sumLos ++;
                    switch (num) {
                        case 1:
                            System.out.println("您出的是石头,电脑出的是布!");
                            break;
                        case 2:
                            System.out.println("您出的是剪刀,电脑出的是石头!");
                            break;
                        case 3:
                            System.out.println("您出的是布,电脑出的是剪刀!");
                            break;
                    }
                }
                System.out.println("您已经获胜"+sumVic+"局,失败"+sumLos+"局,打平"+sumPj+"局");
                System.out.println("\n");
            }
        }
    }

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值