黑马程序员技术交流社区

标题: 猜数字小游戏 [打印本页]

作者: kuro    时间: 2016-4-7 23:28
标题: 猜数字小游戏
今天学到了使用API,借助Math类来做了个小游戏啦啦啦
  1. import java.util.Scanner;
  2. class Test_Math {
  3.         public static void main(String[] args) {
  4.                 int COUNT = 8;
  5.                 Scanner sc = new Scanner(System.in);
  6.                 boolean flag = false;
  7.                 while (true) {
  8.                         int monster = (int)(Math.random() * 100 + 1);
  9.                         int count = COUNT;
  10.                         System.out.println("请输入一个数,0-100:(你还有" + count + "次机会)");
  11.                         for (int i = 0; i < COUNT; i++) {
  12.                                 int x = sc.nextInt();
  13.                                 count--;
  14.                                 if (x > monster) {
  15.                                         System.out.println("大了");
  16.                                         if (count != 0) {
  17.                                                 System.out.println("请输入一个数,0-100:(你还有" + count + "次机会)");
  18.                                         }
  19.                                 } else if (x < monster) {
  20.                                         System.out.println("小了");
  21.                                         if (count != 0) {
  22.                                                 System.out.println("请输入一个数,0-100:(你还有" + count + "次机会)");
  23.                                         }
  24.                                 } else {
  25.                                         System.out.println("中了");
  26.                                         flag = true;
  27.                                         break;
  28.                                 }
  29.                         }
  30.                         System.out.println("[" + monster + "]");
  31.                         if (flag == false) {
  32.                                 System.out.println("没猜中");
  33.                         }

  34.                         System.out.println("继续吗?1:继续 0:退出");
  35.                         int i = sc.nextInt();
  36.                         if (i == 1) {
  37.                                 System.out.println("-----------------------------------");
  38.                                 continue;
  39.                         } else if (i == 0) {
  40.                                 break;
  41.                         } else {
  42.                                 System.out.println("输入有误,系统崩溃,即将退出");
  43.                                 break;
  44.                         }
  45.                 }
  46.         }
  47. }
复制代码

作者: clvslyf    时间: 2016-4-8 00:19
猜数字小游戏,二分查找法,以后还有模拟斗地主小游戏
作者: yaolv7    时间: 2016-4-8 00:44
水货,删了
作者: 性感不是骚    时间: 2016-4-8 00:53
猜拳游戏
作者: huweixiong    时间: 2016-4-8 00:58
看着有点绕,代码写的不是很简洁
作者: 兵蜂    时间: 2016-4-8 08:26
很不错!!!
作者: 251855915    时间: 2016-4-8 14:39
还能斗地主?
作者: yy0328    时间: 2016-4-8 18:03
怎么冒粗来这么多代码
作者: 张世聪    时间: 2016-4-8 20:40
{:2_30:}来个色子游戏。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2