package pratice; import java.util.Random; import java.util.Scanner; public class GameSSS { public static void main(String[] args) { //初始化,定义双方手势变量字符串 Scanner scanner = new Scanner(System.in); System.out.println("欢迎来到剪刀石头布游戏!n请输入剪刀、石头或布:"); //我方手势 String pose = scanner.nextLine(); if (!pose.equals("石头") && !pose.equals("布") && !pose.equals("剪刀")) { System.out.println("输入无效,游戏结束"); return; } //对方手势 String rivalPose; //结果 int flag = 0; //模拟随机出拳 Random random = new Random(); double ran = random.nextDouble(); if (ran > 2.0 / 3) rivalPose = "剪刀"; else if (ran <= 2.0 / 3 && ran > 1.0 / 3) rivalPose = "石头"; else rivalPose = "布"; //计算结果 switch (pose) { case "剪刀": { if (rivalPose.equals("剪刀")) flag = 0; else if (rivalPose.equals("石头")) flag = -1; else flag = 1; break; } case "石头": { if (rivalPose.equals("石头")) flag = 0; else if (rivalPose.equals("布")) flag = -1; else flag = 1; break; } case "布" : if (rivalPose.equals("布")) flag = 0; else if (rivalPose.equals("剪刀")) flag = -1; else flag = 1; break; } if (flag == 0) System.out.println("对手出的是" + rivalPose + ", 平局。"); else if (flag == 1) System.out.println("对手出的是" + rivalPose + ", 你赢了。"); else System.out.println("对手出的是" + rivalPose + ", 你输了。"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)