java 中的模式使用数字
Pattern in java using numbers
我正在尝试打印以下模式,但不明白如何打印。你能帮帮我吗?我正在使用 BlueJ,这是我的第一个问题,所以我不确定需要什么.
1
2 2
3 3 3 3
4 4 4 4 4 4
提前谢谢你。
我试过了
public class Program92
{
public static void main()
{
for(int x=1;x<=5;x++)
{
for(int j=1;j<=x;j++)
System.out.print(x);
System.out.println();
}
}
}
但只能得到
1
22
333
4444
55555
因为这似乎是家庭作业,看看你是否可以设法用这些数字构建一个直角三角形,所以 1
在上面,2 2
在它下面, 4 3
在下面,等等:
1
2 2
3 3 3 3
4 4 4 4 4 4
一旦你做到了,所有你需要做的就是计算出你需要在每个数字之前添加多少个白色space值。
您可以试试这段代码,因为它会先打印空白 space,然后再打印您的号码。而且每个数字后面都有空白 space.
public static void main(String[] args) {
int l= 5;int k=0;
for(int x=1;x<5;x++)
{
for(int i=l*2-1;i>0;i--)
{
if(x == 1 && i ==1)
break;
System.out.print(" ");
}
System.out.print(x);
System.out.print(" ");
for(int i=1;i<x*2-2;i++)
{
System.out.print(x);
System.out.print(" ");
}
System.out.println();
l--;
}
}
试试这个:
public class program98
{
public static void main()
{
System.out.print(" "+"1");//5 spaces int the blank
for(int i=1;i<=4;i++)
{
for(int s=6;s>1;s--)
{System.out.print(" ");//1 space
}
for(int j=1;j<1;j++)
{System,out.print(i);
}
for(int j=1;j<1;j++)
{System.out.print(i);//prints this twice. Hence,instead of once,the number of times it prints is double
}
System.out.println(" ");//1 space
}
}
}
我正在尝试打印以下模式,但不明白如何打印。你能帮帮我吗?我正在使用 BlueJ,这是我的第一个问题,所以我不确定需要什么.
1
2 2
3 3 3 3
4 4 4 4 4 4
提前谢谢你。
我试过了
public class Program92
{
public static void main()
{
for(int x=1;x<=5;x++)
{
for(int j=1;j<=x;j++)
System.out.print(x);
System.out.println();
}
}
}
但只能得到
1
22
333
4444
55555
因为这似乎是家庭作业,看看你是否可以设法用这些数字构建一个直角三角形,所以 1
在上面,2 2
在它下面, 4 3
在下面,等等:
1
2 2
3 3 3 3
4 4 4 4 4 4
一旦你做到了,所有你需要做的就是计算出你需要在每个数字之前添加多少个白色space值。
您可以试试这段代码,因为它会先打印空白 space,然后再打印您的号码。而且每个数字后面都有空白 space.
public static void main(String[] args) {
int l= 5;int k=0;
for(int x=1;x<5;x++)
{
for(int i=l*2-1;i>0;i--)
{
if(x == 1 && i ==1)
break;
System.out.print(" ");
}
System.out.print(x);
System.out.print(" ");
for(int i=1;i<x*2-2;i++)
{
System.out.print(x);
System.out.print(" ");
}
System.out.println();
l--;
}
}
试试这个:
public class program98
{
public static void main()
{
System.out.print(" "+"1");//5 spaces int the blank
for(int i=1;i<=4;i++)
{
for(int s=6;s>1;s--)
{System.out.print(" ");//1 space
}
for(int j=1;j<1;j++)
{System,out.print(i);
}
for(int j=1;j<1;j++)
{System.out.print(i);//prints this twice. Hence,instead of once,the number of times it prints is double
}
System.out.println(" ");//1 space
}
}
}