(Java)嵌套循环按要求输出数字序列
2013-11-29 16:37
295 查看
1、显示数字构成的金字塔,如下
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
实现代码:
2、显示数字构成的金字塔,如下
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
实现代码:
public class PrintPyramid2 {
public static void main(String[] args){
int numberofLines=6;
for(int row=0;row<numberofLines;row++){
for(int column=1;column<=numberofLines-row;column++)
System.out.print(" ");
for(int num=0;num<row;num++){
System.out.print((Math.pow(2, num)>=10)?" "+(int)Math.pow(2, num):" "+(int)Math.pow(2, num));
}
for(int num2=row;num2>=0;num2--)
System.out.print((Math.pow(2, num2)>=10)?" "+(int)Math.pow(2, num2):" "+(int)Math.pow(2, num2));
System.out.println();
}
}
}
3、输出左下三角,如下
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
实现代码:
public class Print1 {
public static void main(String[] args){
int row=6;
for(int i=1;i<=row;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+j);
}
System.out.println();
}
}
}
4、输出左上三角,如下
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
实现代码:
5、输出右下三角,如下
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
实现代码:
6、输出右上三角,如下
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
实现代码:
public class Print4 {
public static void main(String[] args){
int row=6;
for(int i=1;i<=row;i++){
for(int j=1;j<=2*i;j++)
System.out.print(" ");
for(int j=1;j<=row+1-i;j++)
System.out.print(j+" ");
System.out.println();
}
}
}
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
实现代码:
public class PrintPyramid { public static void main(String[] args){ int numberofLines=10; for(int row=1;row<=numberofLines;row++){ for(int column=1;column<=numberofLines-row;column++) System.out.print(" ");//3个领头空格 for(int num=row;num>=1;num--) System.out.print((num>=10)?" "+num:" "+num);//大于10,数字前一个空格 for(int num=2;num<=row;num++) System.out.print((num>=10)?" "+num:" "+num);//小于10,数字前两个空格 System.out.println(); } } }
2、显示数字构成的金字塔,如下
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
实现代码:
public class PrintPyramid2 {
public static void main(String[] args){
int numberofLines=6;
for(int row=0;row<numberofLines;row++){
for(int column=1;column<=numberofLines-row;column++)
System.out.print(" ");
for(int num=0;num<row;num++){
System.out.print((Math.pow(2, num)>=10)?" "+(int)Math.pow(2, num):" "+(int)Math.pow(2, num));
}
for(int num2=row;num2>=0;num2--)
System.out.print((Math.pow(2, num2)>=10)?" "+(int)Math.pow(2, num2):" "+(int)Math.pow(2, num2));
System.out.println();
}
}
}
3、输出左下三角,如下
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
实现代码:
public class Print1 {
public static void main(String[] args){
int row=6;
for(int i=1;i<=row;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+j);
}
System.out.println();
}
}
}
4、输出左上三角,如下
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
实现代码:
public class Print2 { public static void main(String[] args){ int row=6; for(int i=1;i<=row;i++){ for(int j=1;j<=row+1-i;j++){ System.out.print(" "+j); } System.out.println(); } } }
5、输出右下三角,如下
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
实现代码:
public class Print3 { public static void main(String[] args){ int row=6; for(int i=1;i<=row;i++){ for(int j=2*(row-i);j>0;j--) System.out.print(" "); for(int j=i;j>0;j--) System.out.print(j+" "); System.out.println(); } } }
6、输出右上三角,如下
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
实现代码:
public class Print4 {
public static void main(String[] args){
int row=6;
for(int i=1;i<=row;i++){
for(int j=1;j<=2*i;j++)
System.out.print(" ");
for(int j=1;j<=row+1-i;j++)
System.out.print(j+" ");
System.out.println();
}
}
}
相关文章推荐
- 将数字序列逆序输出(java)
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- java中给出一个不多于5位数的正整数,要求,第一是求出它是第多少位的,逆序打印出各位数字
- 构造二叉树的抽象数据类型对于给定的先序序列和中序序列,构造二叉树,并按层输出所有结点内容,要求每层结点输出一行按层输出上述二叉树所表示的森林的所有结点内容
- Java数字格式化输出时前面补0
- Java数组求平均数且输出大于平均数的数字
- 随意输入一个正整数n, 在输入转化的进制数,将转化后的序列输出,并判断某一个数字的个数,这里给的是二进制的例子
- 将1~6这6个数字按每行3个进行输出,输出时要求左边的数字比右边的大,上边的数字比下边的大,求出所有可能的填写方法并统计输出排列方法有多少种。
- java://Comparator、Comparable的用法(按照要求将map集合的键值对进行顺序输出)
- 【c语言】输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一个空格。如:1990->1 9 9 0
- java将小写数字变成大写输出
- 输入是一个只包含拼音的字符串,请输出对应的数字序列。
- 习题 7.8 写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一个空格。如输入1990,应输出“1 9 9 0”。
- java输出吸血鬼数字小结
- java 实现接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写
- 给一个不多于五位的正整数,要求: (1)求出他是几位数 (2)分别输出每位数字 (3)按逆序输出各位数字
- 随机生成一个十位数字序列,要求各位不相同
- 给一个不多于5位的正整数,要求:1、求出它是几位数;2、分别打印出每一位数字; 3、按逆序打印出各位数字,例如原数为321,应输出123。
- 关于一道J笔试或者机试题的Java实现:从键盘输入一串字符,翻转后输出(要求不使用string相关类即对象)
- java 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。