博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
猜拳游戏项目(涉及知识点Scanner、Random、For、数组、Break、Continue等)
阅读量:4496 次
发布时间:2019-06-08

本文共 1948 字,大约阅读时间需要 6 分钟。

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

你可能感兴趣的文章
HDU 1709 The Balance
查看>>
2016/7/7 设置wamp2.5 mysql密码 重点是mysql版本
查看>>
简介几种负载均衡原理
查看>>
micropython logging文档
查看>>
Webform(分页、组合查询)
查看>>
Foundation - NSDate
查看>>
geatpy - 遗传和进化算法相关算子的库函数(python)
查看>>
iOS 线程安全
查看>>
mysql 分组之后统计记录条数
查看>>
New STL Algorithms That Will Make A More Productive Developer
查看>>
js 对象 浅拷贝 和 深拷贝
查看>>
初识 python
查看>>
PCL Examples
查看>>
spring boot
查看>>
浏览器URL传参最大长度问题
查看>>
学习进度条
查看>>
Linux crontab 定时任务详解
查看>>
string成员函数
查看>>
onSaveInstanceState()方法问题
查看>>
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>