请指点一二!
请问在java中如何显示以下图形10000000001
01000000010
00100000100
00010001000
00001010000
00000100000
00001010000
00010001000
00100000100
01000000010
10000000001
----------------解决方案--------------------------------------------------------
public statc void main(String args[])
{
System.out.println("100000000001");
.....
....
.....
....
}
别介意。。。玩笑。。。
----------------解决方案--------------------------------------------------------
程序代码:
public class BinaryString {
public static void main(String[] args) {
int nums[] = {0x401, 0x202, 0x104, 0x88, 0x50, 0x20, 0x50, 0x88, 0x104, 0x202, 0x401};
for(int i = 0; i < nums.length; i++)
System.out.println(toBinaryString(nums[i], 11));
}
public String toBinaryString(int num, int digits) {
StringBuilder sb = new StringBuilder();
for(int i = digits; i > -1; i--)
sb.append(num >>> i & 1);
return sb.toString();
}
}
public static void main(String[] args) {
int nums[] = {0x401, 0x202, 0x104, 0x88, 0x50, 0x20, 0x50, 0x88, 0x104, 0x202, 0x401};
for(int i = 0; i < nums.length; i++)
System.out.println(toBinaryString(nums[i], 11));
}
public String toBinaryString(int num, int digits) {
StringBuilder sb = new StringBuilder();
for(int i = digits; i > -1; i--)
sb.append(num >>> i & 1);
return sb.toString();
}
}
----------------解决方案--------------------------------------------------------
用两个for循环!
----------------解决方案--------------------------------------------------------
回复 3楼 lz1091914999
写得太好了。。。。佩服 ----------------解决方案--------------------------------------------------------
for(int i=0;i<=10;i++)
{
if(i<6)
{
for(int j=0;j<i;j++)
{
System.out.print(0);
}
for(int n=i;n<=i;n++)
{
System.out.print(1);
}
for(int t=i+1;t<=9-i;t++)
{
System.out.print(0);
}
if(i!=10-i)
{
for(int a=10-i;a<=10-i;a++)
{
System.out.print(1);
}
}
for(int b=10-i;b<10;b++)
{
System.out.print(0);
}
}
else
{
for(int j=0;j<10-i;j++)
{
System.out.print(0);
}
for(int n=10-i;n<=10-i;n++)
{
System.out.print(1);
}
for(int t=10-i+1;t<=i-1;t++)
{
System.out.print(0);
}
for(int a=i;a<=i;a++)
{
System.out.print(1);
}
for(int b=i;b<10;b++)
{
System.out.print(0);
}
}
System.out.println();
}
----------------解决方案--------------------------------------------------------