62,621
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.JOptionPane;
public class test3 {
public static void main(String[] args) {
String num=JOptionPane.showInputDialog("请输入杨辉三角形的行数:50以内的数字");
try{
int fei= Integer.parseInt(num);
}
catch (NumberFormatException e){
System.out.println("请输入数字,不可为空");
return;
}
int n = Integer.valueOf(num);
String hou ="%16d";
String xiao =" ";
Long c=0l;
if(n>50)
System.out.print("请输入正确的值");
else {
/* if(n<=10){ hou="%6d"; xiao=" ";}
else if(n<=20){ hou="%10d"; xiao=" ";}
else if(n<=30) {hou="%12d"; xiao=" ";}
else if(n<=40) {hou="%14d"; xiao=" ";} */
long[][] a=new long[n][n];
for(int i=0;i<n;i++)
{
a[i][0]=1;
a[i][i]=1;
}
for(int i=2;i<n;i++) {
for(int j=1;j<i;j++)
{ //这里少了大括号
a[i][j]=a[i-1][j-1]+a[i-1][j];
c = (a[i][j])/2;
}
}
String d = c.toString();
for (int e =1; e<=d.length(); e++)
//hou="&"+"e"+"d"; //这里也有问题
hou="%" + e + "d";
xiao=" "+" ";
for(int i=0;i<n;i++)
{
//打印空格
for(int k=0;k<n-i;++k)
System.out.print(xiao);
//打印数字
for(int j=0;j<=i;j++)
System.out.printf(hou,a[i][j]);
System.out.println(" ");
//换行
System.out.println();
}
}
}
}