二进制到字符矩阵帮助

发布于 2024-08-20 16:30:21 字数 1152 浏览 3 评论 0原文

仅供参考:这是一个练习作业。我一直在努力,但现在我陷入了困境。任何提示/帮助将不胜感激。我盯着它看了一会儿,却没有任何进展。

总结问题: 九枚硬币放置在 3x3 矩阵中,一些面朝上,一些面朝下。头 = 0,尾 = 1。每个状态也可以使用二进制数表示。有512种可能性。问题:编写一个程序,要求用户输入 0-511 之间的数字,并显示带有字符 H 和 T 的相应矩阵,如下所示:

用户输入数字 7(即 000000111 或 HHHHHHTTT) 显示应该是: 呼呼呼 呼呼呼 TTT

这就是我到目前为止所拥有的。我不一定要寻求答案,我只是想朝正确的方向推动。谢谢

import java.util.Scanner;

public class converting {
    public static void main(String[] ar) {

    Scanner s = new Scanner(System.in);

        System.out.print("Enter a number between 0 and 511: ");

        int number = s.nextInt();
        if(number <= 511 && number > 0)
        {
             String bin = Integer.toBinaryString(number);
             String tails = bin.replace('1', 'T');

         int count = 0;
         char[] arr = tails.toCharArray();

         for (int i = 0; i < arr.length; i++) {

            System.out.print(arr[i]);
            count++;
            if (count == 3) {
                System.out.println();
                count = 0;
            }
        }
      }
      else{
        System.out.print("Please enter a number between 0 and 511\n");
    }
    }
}

FYI: This is a practice homework exercise. I've been working on it, but now I'm stuck. Any tips/help would be appreciated. I've been staring at it a while and no progress has been made.

Summarized question:
Nine coins are place in a 3x3 matrix with some face up and some face down. Heads = 0 and tails = 1. Each state can also be represented using a binary number. There are 512 possibilities. Problem: Write a program that asks user for a number between 0-511 and displays corresponding matrix with characters H and T like this:

User enters number 7 (which is 000000111 or HHHHHHTTT)
Display should be:
H H H
H H H
T T T

This is what I have so far. I'm not asking for the answer necessarily, I would just like a push in the right direction. Thanks

import java.util.Scanner;

public class converting {
    public static void main(String[] ar) {

    Scanner s = new Scanner(System.in);

        System.out.print("Enter a number between 0 and 511: ");

        int number = s.nextInt();
        if(number <= 511 && number > 0)
        {
             String bin = Integer.toBinaryString(number);
             String tails = bin.replace('1', 'T');

         int count = 0;
         char[] arr = tails.toCharArray();

         for (int i = 0; i < arr.length; i++) {

            System.out.print(arr[i]);
            count++;
            if (count == 3) {
                System.out.println();
                count = 0;
            }
        }
      }
      else{
        System.out.print("Please enter a number between 0 and 511\n");
    }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆追雨的时光 2024-08-27 16:30:21

你真的很接近。一些注意事项:

  • Scanner#nextInt 可能会抛出异常;优雅地对待他们。
  • 您需要检查 number 是否在范围 (0-511) 内。
  • 有了 bin 后,您就拥有了 1-9 个二进制数字 - 01。您需要确保正好有 9 个,因此请在前面插入任何缺失的 0
  • 您现在有 9 个 01;您需要 HT。查看 String#replace
  • 您现在拥有一个包含 9 个 HT 的字符串。分三行输出,每行三个字符。查看 字符串#substring

You're really close. Some notes:

  • Scanner#nextInt can throw exceptions; handle them gracefully.
  • You need to check that number is in range (0-511).
  • Once you have bin, you have 1-9 binary digits -- 0s and 1s. You want to make sure you have exactly 9 of them, so insert any missing 0s at the front.
  • You now have 9 0s and 1s; you want Hs and Ts. Check out String#replace.
  • You now have a string with 9 Hs and Ts. Output it in three lines, three chars per line. Check out String#substring.
君勿笑 2024-08-27 16:30:21

intToString.toCharArray();

我认为这应该是 bin.toCharArray() 。

您还有什么问题吗?

intToString.toCharArray();

This should be bin.toCharArray(), methinks.

What else are you having problems with?

愚人国度 2024-08-27 16:30:21

String.replace(CharSequence, CharSequence) 在这里可能有用。

String.replace(CharSequence, CharSequence) may be useful here.

我家小可爱 2024-08-27 16:30:21
  • 我会使用 StringBuilder 构建“板”,然后才将其打印出来。
  • 我将创建一个名为“createBoard”的方法,其参数为数量、宽度和深度。始终保持灵活性。
  • 我将使用两个计数器,例如 x 和 y 来迭代宽度和深度(一个简单的 for next 循环就足够了)。 position = x * width + y
  • 我会使用 BigInteger.valueOf() 并使用 BigInteger.testBit(position)。
  • 我会确保我的代码看起来不错并且有注释。
  • I would use StringBuilder to build up the "board", and only print it out afterwards.
  • I would create a method called "createBoard" that has as parameters the number, the width and the depth. Always program for flexibility.
  • I would use two counters, say x and y that iterate through the width and depth (a simple for next loop suffices). position = x * width + y
  • I would use BigInteger.valueOf() and use BigInteger.testBit(position).
  • I would make really sure that my code looks nice and has comments in.
  • ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文