我想问JAVA语言 数字排列一个形状有什么 诀窍?

来源:百度知道 编辑:UC知道 时间:2024/06/18 05:29:56
我是初学者 我老师给我出了几个题目
1
1 2
1 2 3
1 2 3 4
还有
1
2 1
3 2 1
4 3 2 1
继续
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1

就这样的~第一个我想出来了~
int a=1;
int n=4;
int j=1;
for(;a<=n;a++)
{
for(j=1;j<=a;j++)
{
System.out.print(j+"\t");
}
System.out.println();
}

但是后面的 我不知道怎么 先空格后输入结果
1
第一排可以打出 但是第2排我不知道如何设置变量,忘高手解答 小弟谢谢了~

2:
public static void main (String[] args) {
int a=1;
int n=4;
int j=1;
for(;a<=n;a++)
{
for(j=a;j>=1;j--)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
-------------------------
3.
public static void main (String[] args) {
int a=1;
int n=4;
int j=1;
for(;a<=n;a++)
{
for(j=1;j<=a;j++)
{
System.out.print(j+"\t");
}
for(j=a-1;j>=1;j--)
{
System.out.print(j+"\t");
}
System.out.println();
}
-------------------------
这是不是你要的?
ps:不要学的太死,要灵活运用~