java实现数字炸弹

数字炸弹游戏规则

数字炸弹游戏规则:在一个数字范围内,有一个数字作为炸弹,谁猜中这个炸弹就被惩罚.比如范围是1 ~ 99,炸弹是60,然后猜了一个数字是30,30不是炸弹,那么现在猜数字的范围就缩小到30 ~ 100, 又猜了一个数字80,80也不是炸弹,那么现在又缩小范围到30~80,每次猜不能猜边界上的值,直到有人猜中这个炸弹,然后就受到惩罚。

Java实现

实现思路:随机生成一个0~100的整数bomb,定义初始边界front=0,behind=100。输入猜的数字,当猜的数字不等于炸弹数字,就一直做循环体,当猜的数字等于炸弹数字,发生爆炸。

Java代码:

import java.util.Scanner;

/**
 * @Author:yxq
 * @Date: 2020/7/16 14:36
 * @Tools: IntelliJ IDEA
 **/

/*
 数字炸弹
 */
public class DigitalBomb {


 public static void main(String[] args) {
  int bomb = (int) (100 * Math.random());  //定义随机炸弹数
  int front = 0, behind = 100;  //定义范围边界

  Scanner input = new Scanner(System.in);
  System.out.println("你猜的数是(0~100):");
  int guess = input.nextInt();   //输入猜的数字

  while (guess != bomb) {
   if (guess > bomb) {
    System.out.println("在"+front + "~" + guess+"之间");
    behind = guess;  //若猜的数大于炸弹数字,那么将猜的数作为最大边界
    System.out.println("继续猜:");
    guess = input.nextInt();
   } else {
    System.out.println("在"+guess + "~" + behind+"之间");
    front = guess;  //若猜的数小于炸弹数字,那么将猜的数作为最小边界
    System.out.println("继续猜:");
    guess = input.nextInt();
   }
  }

  if (guess == bomb) {
   System.out.println("\\\\\\!!!!!!!!!!!!!!!//////");
   System.out.println("------!!!!!BOOM!!!!!!------");
   System.out.println("//////!!!!!!!!!!!!!!!\\\\\\");
   System.out.println("炸弹数字就是"+bomb);
  }

 }
}

其实也蛮简单的。今日刷抖音刷到玩数字炸弹的,所以试着使用java实现这么一个简单的数字炸弹游戏。

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。