A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kuro 中级黑马   /  2016-4-7 23:28  /  481 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天学到了使用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. }
复制代码

8 个回复

倒序浏览
猜数字小游戏,二分查找法,以后还有模拟斗地主小游戏
回复 使用道具 举报
水货,删了
回复 使用道具 举报
猜拳游戏
回复 使用道具 举报
看着有点绕,代码写的不是很简洁
回复 使用道具 举报
很不错!!!
回复 使用道具 举报
还能斗地主?
回复 使用道具 举报
怎么冒粗来这么多代码
回复 使用道具 举报
{:2_30:}来个色子游戏。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马