public static void main(String[] args) { int a = 0; String align = ""; for (int i = 0; i < 6; i++) { a += 1 << i; align += "0"; }
String[] r = new String[a + 1]; for (int i = 0; i <= a; ++i) { String t = Integer.toBinaryString(i); String s = (align + t); s = s.substring(t.length()); System.out.println(s); } }
public class Test { public static void main(String[] args) { String[] strs = getBinaries(3); for(int i = 0; i < strs.length; i++) { System.out.println(strs[i]); } }
public static String[] getBinaries(int len) { if(len < 0) { throw new IllegalArgumentException("len must be positive number"); } if(len > 30) { throw new IllegalArgumentException("len is too many"); } char[] chs = new char[len]; int count = 1 << len; String[] strs = new String[count]; for(int i = 0; i < count; i++) { strs[i] = getBinary(i, chs); } return strs; }
private static String getBinary(long num, char[] chs) { for(int i = 0, k = chs.length - 1; i <= k; i++) { chs[k - i] = (char)((num & 1) + '0'); num >>= 1; } return new String(chs); } }
public class Test { public static void main(String[] args) { int value = 3; int maxValue = (int)Math.pow(2, 3); String[] result = new String[maxValue]; while(maxValue > 0) { result[maxValue -- - 1] = String.format("%3s", Integer.toBinaryString(maxValue)).replaceAll(" ", "0"); } for(int i = 0; i < result.length; i ++) { System.out.println(result[i]); } } }
来个简单一点的: import java.text.DecimalFormat;public class Gen { public static String genStr(int len) { for (int i = 0; i < (1 << len); i++) { System.out.println(new DecimalFormat("000").format(Double.parseDouble(Integer.toBinaryString(i)))); } return null; } public static void main(String[] args) { Gen.genStr(3); }}
import java.text.DecimalFormat;public class Gen { public static String genStr(int len) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { sb.append("0"); } for (int i = 0; i < (1 << len); i++) { System.out.println(new DecimalFormat(sb.toString()).format(Double.parseDouble(Integer.toBinaryString(i)))); } return null; } public static void main(String[] args) { Gen.genStr(3); } }
import java.text.*;public class FindPrime { public static void toString(int i){ int max = (int)Math.pow(2, i);
String str = Integer.toBinaryString(max);
int maxLength = str.length();
StringBuffer sb = new StringBuffer();
for(int j=0; j<maxLength-1; j++) sb.append("0");
DecimalFormat df = new DecimalFormat(sb.toString());
然后用
Integer.toBinaryString(int i)
就行了
public static void main(String[] args) {
int a = 0;
String align = "";
for (int i = 0; i < 6; i++) {
a += 1 << i;
align += "0";
}
String[] r = new String[a + 1];
for (int i = 0; i <= a; ++i) {
String t = Integer.toBinaryString(i);
String s = (align + t);
s = s.substring(t.length());
System.out.println(s);
}
}
import java.util.Scanner;public class Test_2 {
static public void print(double i){
String[] init=new String[(int)Math.pow(2.0, i)];
DecimalFormat df=new DecimalFormat("000");
for(int a=0;a<init.length;a++){
init[a]=df.format(Double.parseDouble(Integer.toBinaryString(a)));
System.out.println(init[a]);
}
}
public static void main(String[] args) {
Scanner sca=new Scanner(System.in);
double i=sca.nextDouble();
print(i); }}输入3的时候的测试结果:
000
001
010
011
100
101
110
111
String[] strs = getBinaries(3);
for(int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
}
public static String[] getBinaries(int len) {
if(len < 0) {
throw new IllegalArgumentException("len must be positive number");
}
if(len > 30) {
throw new IllegalArgumentException("len is too many");
}
char[] chs = new char[len];
int count = 1 << len;
String[] strs = new String[count];
for(int i = 0; i < count; i++) {
strs[i] = getBinary(i, chs);
}
return strs;
}
private static String getBinary(long num, char[] chs) {
for(int i = 0, k = chs.length - 1; i <= k; i++) {
chs[k - i] = (char)((num & 1) + '0');
num >>= 1;
}
return new String(chs);
}
}
public static void main(String[] args) {
int value = 3;
int maxValue = (int)Math.pow(2, 3);
String[] result = new String[maxValue];
while(maxValue > 0) {
result[maxValue -- - 1] = String.format("%3s", Integer.toBinaryString(maxValue)).replaceAll(" ", "0");
}
for(int i = 0; i < result.length; i ++) {
System.out.println(result[i]);
}
}
}
import java.text.DecimalFormat;public class Gen
{
public static String genStr(int len)
{
for (int i = 0; i < (1 << len); i++)
{
System.out.println(new DecimalFormat("000").format(Double.parseDouble(Integer.toBinaryString(i))));
}
return null;
} public static void main(String[] args)
{
Gen.genStr(3);
}}
{
public static String genStr(int len)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
{
sb.append("0");
}
for (int i = 0; i < (1 << len); i++)
{
System.out.println(new DecimalFormat(sb.toString()).format(Double.parseDouble(Integer.toBinaryString(i))));
}
return null;
} public static void main(String[] args)
{
Gen.genStr(3);
}
}
public static void toString(int i){
int max = (int)Math.pow(2, i);
String str = Integer.toBinaryString(max);
int maxLength = str.length();
StringBuffer sb = new StringBuffer();
for(int j=0; j<maxLength-1; j++)
sb.append("0");
DecimalFormat df = new DecimalFormat(sb.toString());
for(int j=0; j<max; j++)
System.out.println(df.format(Integer.parseInt(Integer.toBinaryString(j))));
}
public static void main(String args[]){
toString(3);
}
}