杨辉三角形问题

hunter1206 2012-06-17 05:16:16
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";
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();
}
}
}
}

我这个一直不对 我希望通过算出他每行的中值 也就是他最大的数 动态修改每行的空格 和System.out.printf("&某数d",a[i][j]);


望大神帮我看看哪里不对
...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
syueo 2012-06-18
  • 打赏
  • 举报
回复
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();
}
}
}
}


hunter1206 2012-06-18
  • 打赏
  • 举报
回复
String d = c.toString();
for (int e =1; e<=d.length(); e++)
//hou="&"+"e"+"d"; //这里也有问题
hou="%" + e + "d";
xiao=" "+" ";




这里貌似还有问题 最后e还是等于1 我哪里写错了 请大神帮忙看看
hunter1206 2012-06-18
  • 打赏
  • 举报
回复
是我写错了 数已经输出 只是不够齐
hunter1206 2012-06-18
  • 打赏
  • 举报
回复
修正后 这是新跳的错误

Exception in thread "main" java.util.IllegalFormatConversionException: e != java.lang.Long
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at test3.main(test3.java:61)



我是想取 每行中间列的值 貌似取不到 望各位大神教教应该怎么弄
周靖峰 2012-06-17
  • 打赏
  • 举报
回复
一共有两处错误,一处是少了大括号,一处是打印的东西错误
另外空格多少什么的你就自己改吧


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();
}
}
}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧