猜拳游戏项目(涉及知识点Scanner、Random、For、数组、Break、Continue等)

 1 package day03.d3.xunhuankongzhi;
 2 
 3 import java.util.Scanner;
 4 
 5 public class CaiQuan {
 6 
 7     public static void main(String[] args) {
 8         Scanner sc = new Scanner(System.in);
 9         String[] arr = { "石头", "剪刀", "布" };
10         while (true) {
11             System.err.println("请选择你要出的手势:");
12             int you;
13             for(;;){
14                 you = sc.nextInt();
15                 if(you>=0&&you<3){
16                     System.err.println("你出的是" + arr[you]);
17                     break;
18                 }else{
19                     System.err.println("你出的手势无效,请重新选择你要出的手势!");
20                     continue;
21                 }                
22             }
23             
24             int comp = (int) (Math.random() * 3);
25             System.err.println("电脑出的是:" + arr[comp]);
26             padDuan(you, comp);
27             System.err.println("是否继续下一局(Y/N):");
28             String str;
29             boolean bool;
30             for (;;) {
31                 str = sc.nextLine();
32                 if (str.equals("Y")) {
33                     bool = true;
34                     break;
35                 } else if (str.equals("N")) {
36                     bool = false;
37                     break;
38                 } else {
39                     System.err.println("输入有误,请重新输入!是否继续下一局(Y/N):");
40                     continue;
41                 }
42             }
43 
44             if (bool) {
45 
46             } else {
47                 break;
48             }
49 
50         }
51     }
52 
53     public static void padDuan(int x, int y) {
54         if (x == y) {
55             System.err.println("英雄所见略同!");
56         } else if ((x == 0 && y == 1) || (x == 1 && y == 2)
57                 || (x == 2 && y == 0)) {
58             System.err.println("恭喜你,你赢了!");
59         } else {
60             System.err.println("很遗憾,你输了,请再接再厉!");
61         }
62     }
63 }

 

转载于:https://www.cnblogs.com/1020182600HENG/p/6564883.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值