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