欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

练习一下,写个“排雷”小游戏

程序员文章站 2022-07-14 12:42:24
...

/*
排雷
1.产生随机一个数(0~100];
2.根据提示键盘输入一个数去猜这个数;
3.直到猜中。
*/

import java.util.Random;
import java.util.Scanner;

public class RandomGame {
public static void main(String[] args) {
    Random r = new Random();
    int lei = r.nextInt(100) + 1;


 System.out.println("请输入(0~100]之间的数:");//开始排雷(猜数)
    Scanner sc = new Scanner(System.in);
    int guessNum = sc.nextInt();

    int a = 0;
    int b = 100;

    while (true) {

        if (guessNum > lei) {
            System.out.println("请输入(" + a + "~" + guessNum + ")之间的数");
            b = guessNum;
            int guessAgain = sc.nextInt();
            guessNum = guessAgain;

        } else if (guessNum < lei) {
            System.out.println("请输入(" + guessNum + "~" + b + ")之间的数");
            a = guessNum;
            int guessAgain = sc.nextInt();
            guessNum = guessAgain;


        } else {
            System.out.println("排雷成功!");
            break;
        }
    }
    System.out.println("游戏结束!");


  }
}

练习一下,写个“排雷”小游戏

相关标签: java