简体   繁体   English

Java中的二进制猜谜游戏

[英]Binary Guessing Game In Java

I am trying to create a guessing game that takes an input from a user.我正在尝试创建一个接受用户输入的猜谜游戏。 My issue is, when the user guesses incorrectly, the right digit of he binary number must be revealed, and so on.我的问题是,当用户猜错时,必须显示二进制数的正确数字,依此类推。

However, I am having issues with the order and length of the output.但是,我对输出的顺序和长度有问题。

What I need:我需要的:

RANDOM VALUE GENERATED: 11产生的随机值:11

Binary number to guess: 1011要猜测的二进制数: 1011

Display shown to user: ----显示给用户: ----

Display shown after first incorrect guess: ---1第一次猜错后显示的显示: ---1

Display shown after second incorrect guess: --11后第二猜测不正确所示显示: --11

Display shown after third incorrect guess: -011第三次错误猜测后显示的显示: -011

Display shown after fourth incorrect guess: 1011第四次错误猜测后显示的显示: 1011

What I get:我得到的:

RANDOM VALUE GENERATED: 11产生的随机值:11

Binary number to guess: 1011要猜测的二进制数: 1011

Display shown to user: -------显示给用户: -------

Display shown after first incorrect guess: ------0第一次错误猜测后显示的显示: ------0

Display shown after second incorrect guess: -----00第二次错误猜测后显示的显示: -----00

Display shown after third incorrect guess: ----000第三次猜错后显示: ----000

Display shown after fourth incorrect guess: ---0000第四次错误猜测后显示的显示: ---0000

Display shown after fifth incorrect guess: --10000第五次错误猜测后显示的显示:-- --10000

Display shown after sixth incorrect guess: -010000第六次错误猜测后显示的显示: -010000

Display shown after seventh incorrect guess: 1010000第七次猜错后显示: 1010000

My code:我的代码:

Scanner scan = new Scanner(System.in);

        // Generating a random number between 1-16
        int randomNum = (int) (Math.random() * 16 + 1);
        int original = randomNum;
        System.out.println("Random num generated: " + randomNum);

        // Converting the random number to a binary number
        System.out.print("Random number in binary: ");
        int[] binary = new int[8];
        int originalBinary = 0;
        int index = 0;
        int count = 0;

        while (randomNum > 0) {
            binary[index++] = randomNum % 2;
            randomNum /= 2;
        }
        for (int i = index - 1; i >= 0; i--) {
            count++;

            System.out.print(binary[i]);
            originalBinary = binary[i];
        }
        System.out.println("\n***********************************");

        // System.out.print("\nPlease enter a guess: ");
        int guess = scan.nextInt();
        int digitsToReveal = 0;

        while (guess != original && digitsToReveal != binary.length) {

            System.out.print("\nPlease enter another number: ");
            guess = scan.nextInt();
            System.out.println(reveal(binary, digitsToReveal++));

        }

    }

    public static String reveal(int[] arr, int reveal) {
        StringBuilder str = new StringBuilder();
        for (int i = 1; i < arr.length; i++) {
            if (i >= arr.length - reveal) {
                str.append(arr[i]);
            } else {
                str.append("-");
            }

        }
        return str.toString();
    }
}



Note that your binary array stores the bits of the integer in the reverse order in which you printed it.请注意,您的binary数组以与打印它的相反顺序存储整数的位。 That is, binary[0] is the most significant bit.也就是说, binary[0]是最高有效位。 For example, for the number 13 (binary 1101), the array looks like:例如,对于数字 13(二进制 1101),数组如下所示:

[1, 0, 1, 1, 0, 0, 0, 0]

When printing the binary version of the integer, you correctly read the array in reverse:打印整数的二进制版本时,您可以正确地反向读取数组:

for (int i = index - 1; i >= 0; i--) {
    System.out.print(binary[i]);
}

So in reveal , you should read the array in reverse too:因此,在reveal ,您也应该反向读取数组:

public static String reveal(int[] arr, int reveal) {
    StringBuilder str = new StringBuilder();
    for (int i = 1; i < arr.length; i++) {
        if (i >= arr.length - reveal) {
            // note the change here:
            str.append(arr[arr.length - i - 1]);
        } else {
            str.append("-");
        }

    }
    return str.toString();
}

Rather than i , you should access arr.length - i - 1 .而不是i ,您应该访问arr.length - i - 1

Also, note that an array of length 5 can sufficiently store numbers from 1 to 16, you don't need to use an array of length 8.另请注意,长度为 5 的数组可以充分存储 1 到 16 之间的数字,您不需要使用长度为 8 的数组。


I don't know if this is intentional or not, but you are not revealing the most significant bit.我不知道这是否是故意的,但你没有透露最重要的一点。

Because of you are store binary number in the revers order to binary array variable因为你是以相反的顺序存储二进制数到binary数组变量

Number : 11数量: 11

Binary : 1011二进制1011

array : [1, 1, 0, 1, 0, 0, 0, 0]数组[1, 1, 0, 1, 0, 0, 0, 0]

I fix this by store it at last to first element of an binary array by starting index variable with 7 and index-- in while loop.我通过以7index--在 while 循环中开始index变量,通过将其最后存储到二进制数组的第一个元素来解决此问题。 And I edit digitToReveals = 1 and move reveal calling method to first line in while loop, this will reveal the number since first wrong guessing.我编辑digitToReveals = 1并将reveal调用方法移动到 while 循环中的第一行,这将显示自第一次错误猜测以来的数字。

import java.util.Scanner; // Import the Scanner class

public class GuessFromBinary {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System. in );

    // Generating a random number between 1-16
    int randomNum = (int)(Math.random() * 16 + 1);
    int original = randomNum;
    System.out.println("Random num generated: " + randomNum);

    // Converting the random number to a binary number
    System.out.print("Random number in binary: ");
    int[] binary = new int[8];
    int index = 7;

    while (randomNum > 0) {
      binary[index--] = randomNum % 2;
      randomNum /= 2;
    }

    for (int i = index + 1; i < 8; i++) {
      System.out.print(binary[i]);
    }

    System.out.println("\n***********************************");

    System.out.print("\nPlease enter a guess: ");
    int guess = scan.nextInt();

    int digitsToReveal = 1;

    while (guess != original && digitsToReveal != binary.length) {
      System.out.println(reveal(binary, digitsToReveal++));
      System.out.print("\nPlease enter another number: ");
      guess = scan.nextInt();
    }

    if (guess == original) System.out.print("\nYes!");
    else System.out.print("\nNo luck :(");

  }

  public static String reveal(int[] arr, int reveal) {
    StringBuilder str = new StringBuilder();
    for (int i = 1; i < arr.length; i++) {
      if (i >= arr.length - reveal) {
        str.append(arr[i]);
      } else {
        str.append("-");
      }

    }
    return str.toString();
  }
}

And I also adjust some other things (unused variable, message interactions, ...).我还调整了一些其他的东西(未使用的变量,消息交互,...)。 You give a player 8 times to guess per round if I didn't misunderstand.如果我没有误解,你每轮给一个玩家 8 次猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM