[英]Print pattern using java?
Would you please help how to print below format using java? 您能帮我如何使用Java打印以下格式吗?
0001
0010
0100
1000
Below code is not giving me the desired output. 下面的代码没有给我想要的输出。
public static void main(String[] args) {
if (args.length > 0) {
int n = Integer.parseInt(args[0]);
int m = 1;
for (int i = 1; i <= n; ++i) {
m *= 2;
}
System.out.println("# bits : " + n);
System.out.println("# values: " + m);
String format = "%" + n + "s";
for (int i = 0; i < m; ++i) {
System.out.println(String.format(format, Integer.toString(i, 2)));
}
} else {
System.out.println("Usage: BinaryDemo <n>");
}
}
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.format("%04d%n", Integer.parseInt(Integer.toString((int) Math.pow(2, i), 2)));
}
}
EDIT : 编辑:
public static void main(String[] args) {
int n = 5, padding = findPadding((int) Math.pow(2, n - 1));
for (int i = 0; i < n; i++) {
printBinary((int) Math.pow(2, i), padding);
}
}
static int findPadding(int value) {
int bitCount = Integer.toString(value, 2).length(), padding;
for (int i = 2; ; i++) {
padding = (int) Math.pow(2, i);
if (padding >= bitCount) {
return padding;
}
}
}
static void printBinary(int value, int padding) {
String binaryString = String.format("%0" + padding + "d", Integer.parseInt(Integer.toString(value, 2)));
int i = 0;
for (char ch : binaryString.toCharArray()) {
System.out.print(ch);
if (++i == 4) {
System.out.print(" ");
i = 0;
}
}
System.out.println();
}
OUTPUT : 输出:
0000 0001
0000 0010
0000 0100
0000 1000
0001 0000
you need not to loop through the m
for printing the pattern. 您无需遍历
m
打印图案。 You can form the pattern with the n
user input and print it within the first loop. 您可以使用
n
用户输入来形成图案,并在第一个循环内打印它。
String format = "%0"+n+"d%n";
for (int i = 0; i < n; ++i) {
System.out.format(format, Integer.parseInt(Integer.toString(m, 2)));
m = m << 1;
}
I tried this: 我尝试了这个:
for (int i = 0; i < m; ++i) {
// modified this line - format (above) not needed anymore
// n below refers to the number of bits calculated above so it will pad correctly
System.out.format("%0" + n + "d%n", Integer.parseInt(Integer.toString(i, 2)));
}
Seems to work 似乎可以工作
The String.format
(and the System.out.format
, too) supports a wide variety of patterns. String.format
(以及System.out.format
)也支持多种模式。 You'll have to look at the Javadoc of the Formatter
-Class to find it. 您必须查看
Formatter
-Class的Javadoc才能找到它。
You did nearly the right thing, but the Flag 0
only works for Numbers, so %4s
won't work. 您所做的事几乎是正确的,但是标志
0
仅适用于数字,因此%4s
将不起作用。 With a number conversion ( d
, f
, etc.) like %04d
it works, the output will have additional zeros, if its shorter than 4 digits. 使用
%04d
之类的数字转换( d
, f
等)时,如果输出少于4位,则输出将具有附加的零。 Its also possible to use a blank instead of zeros. 也可以使用空白而不是零。
String pattern = "%04d";
System.out.println(String.format(pattern, 1));
System.out.println(String.format(pattern, 11));
System.out.println(String.format(pattern, 111));
System.out.println(String.format(pattern, 1111));
System.out.println(String.format(pattern, 11111));
Output: 输出:
0001
0011
0111
1111
11111
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.