中易网

在Java中实现数字金字塔

答案:5  悬赏:70  
解决时间 2021-04-18 01:35
***********************************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
****11*10**9**8**7**6**5**4**3**2**1**2**3**4**5**6**7**8**9*10*11
*12*11*10**9**8**7**6**5**4**3**2**1**2**3**4**5**6**7**8**9*10*11*12



最佳答案
你好,代码如下,与你图中的一模一样:
public class Pyramid {


public static String[][] getPyramid(int h){
//根据图像可以观察只
//每一个数据都是有*和数字组成 而且规律是:
//如果数子长度为1,那么前面有两个*
//如果数子长度为2,那么前面有一个*
//根据高度来计算数组的宽度
int w = 1 + (h-1)*2 ;
//
String[][] array = new String[h][w] ;

for(int i = 0;i<h;i++){
for(int j=0;j<w;j++){
if(j<h-i-1){
array[i][j] = "***";
}else if(j<h){
if((h-j)>9){
array[i][j] = "*" + (h-j) ;
}else{
array[i][j] = "**" + (h-j) ;
}
}else if(j<h+i){
if((h-j)>9){
array[i][j] = "*" + (j-h+2) ;
}else{
array[i][j] = "**" + (j-h+2) ;
}
}else{
array[i][j] = "";
}
}
}

return array ;
}

public static void print(String[][] array){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
System.out.print(array[i][j]) ;
}
System.out.println();
}
}

public static void main(String[] args) {
print(getPyramid(12)) ;
}
}
全部回答
import java.util.Scanner ; public class Pyramid { public static void main(String[] args) { System.out.print("Enter the number of lines : ") ; Scanner input = new Scanner(System.in ) ; int m = input.nextInt(); if (m < 1 || m > 15) { System.out.println("You must enter a number from 1 to 15"); System.exit(0); } // Print lines for (int row = 1; row <= m; row++) { // Print NUMBER_OF_LINES - row leading spaces for (int column = 1; column <= m - row; column++) System.out.print("**"); // Print leading numbers row, row -1, ..., 1 for (int num = row; num >= 1; num--) System.out.print((num >= 10) ? "**" + num : "**" + num); // Print ending numbers 2, 3, ..., row - 1, row for (int num = 2; num <= row; num++) System.out.print((num >= 10) ? "**" + num : " **" + num); // Start a new line System.out.println(); } } }
杨辉三角形 主要用FOR循环来做
这个叫做杨辉三角形 算法网上有现成的 你可以先实现打印菱形图案 以后多交流 谢谢请给最佳
现编的 完全符合你的要求 你看看吧 import java.util.*; public class Test { public static void main(String[] args) { System.out.println("输入一所需要三角形层数"); int a = new Scanner(System.in).nextInt(); for(int i=1; i<=a; i++) { for(int j=(a-i)*3-1; j>=0; j--) { System.out.print("*"); } for(int j=i; j>=1; j--) { if(j<10)System.out.print("**"); else System.out.print("*"); System.out.print(j); } for(int j=2; j<=i; j++) { if(j<10)System.out.print("**"); else System.out.print("*"); System.out.print(j); } System.out.println(""); } } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
从源深路655号源深体育中心到宜川有什么公交
承德到长春有蹦极的地吗
华润燃气灶具价格crb-116s
有没有哪位亲和我一样吃了那个美国红毒溶脂。
现在DNF怎么挂这么多,有次组的4人队就有2个外
一到高一化学题
韩国roommate第一季好看吗
6120C停产没?郑州地区得多少钱?
谁能帮我把“我爱杨波”组一个字迷?谢谢了
丹阳市机关事业单位退离休人员的工资为什么没
急,希望有人快点回答
卡帕的书包好不好
爱宠国最近要推出新道具和新品种犬类,那么以
同事的儿子结婚, 我在红包是只写“新婚志禧
中国好声音是什么时候开始的
推荐资讯
成都最靠谱的埋线瘦身在哪?
一个男生说我引爆了他,是什么意思?
梦见老公顶雨去找我
涮串的底料用火锅底料行吗
淮北段园镇派出所吃、喝、要、拿,无人敢举,
對下联:村子里死了个小孩;
咋弄升级快
河南一区的这万仞多少钱
爱我的人我不爱,我爱的人不爱我,这样的爱情
淘宝购物,下单后,卖家已经发了货,货正在来
什么产品可以达到瘦脸的效果,韩国瘦脸产品可
女人是不是都喜欢攀比呀
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?