二进制到字符矩阵帮助
仅供参考:这是一个练习作业。我一直在努力,但现在我陷入了困境。任何提示/帮助将不胜感激。我盯着它看了一会儿,却没有任何进展。
总结问题: 九枚硬币放置在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你真的很接近。一些注意事项:
Scanner#nextInt
可能会抛出异常;优雅地对待他们。number
是否在范围 (0-511) 内。bin
后,您就拥有了 1-9 个二进制数字 -0
和1
。您需要确保正好有 9 个,因此请在前面插入任何缺失的0
。0
和1
;您需要H
和T
。查看String#replace
。H
和T
的字符串。分三行输出,每行三个字符。查看字符串#substring
。You're really close. Some notes:
Scanner#nextInt
can throw exceptions; handle them gracefully.number
is in range (0-511).bin
, you have 1-9 binary digits --0
s and1
s. You want to make sure you have exactly 9 of them, so insert any missing0
s at the front.0
s and1
s; you wantH
s andT
s. Check outString#replace
.H
s andT
s. Output it in three lines, three chars per line. Check outString#substring
.我认为这应该是 bin.toCharArray() 。
您还有什么问题吗?
This should be
bin.toCharArray()
, methinks.What else are you having problems with?
String.replace(CharSequence, CharSequence)
在这里可能有用。String.replace(CharSequence, CharSequence)
may be useful here.