黑马程序员技术交流社区
标题:
猜数字小游戏
[打印本页]
作者:
kuro
时间:
2016-4-7 23:28
标题:
猜数字小游戏
今天学到了使用API,借助Math类来做了个小游戏啦啦啦
import java.util.Scanner;
class Test_Math {
public static void main(String[] args) {
int COUNT = 8;
Scanner sc = new Scanner(System.in);
boolean flag = false;
while (true) {
int monster = (int)(Math.random() * 100 + 1);
int count = COUNT;
System.out.println("请输入一个数,0-100:(你还有" + count + "次机会)");
for (int i = 0; i < COUNT; i++) {
int x = sc.nextInt();
count--;
if (x > monster) {
System.out.println("大了");
if (count != 0) {
System.out.println("请输入一个数,0-100:(你还有" + count + "次机会)");
}
} else if (x < monster) {
System.out.println("小了");
if (count != 0) {
System.out.println("请输入一个数,0-100:(你还有" + count + "次机会)");
}
} else {
System.out.println("中了");
flag = true;
break;
}
}
System.out.println("[" + monster + "]");
if (flag == false) {
System.out.println("没猜中");
}
System.out.println("继续吗?1:继续 0:退出");
int i = sc.nextInt();
if (i == 1) {
System.out.println("-----------------------------------");
continue;
} else if (i == 0) {
break;
} else {
System.out.println("输入有误,系统崩溃,即将退出");
break;
}
}
}
}
复制代码
作者:
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