主题:[讨论]谁帮我看看这那里出问题
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class yanghui extends Applet implements ActionListener{
Label lb1=new Label("请输入要显示的行数:");
TextField txt1=new TextField(4);
Button btn=new Button("确定");
int n=0;
int[][] yang;
public void init(){
add(lb1);
add(txt1);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
n=Integer.parseInt(txt1.getText());
yang=yanghui(n);
repaint();
}
public void paint(Graphics g){
for(int i=0;i<n;i++){
String s=" ";
for(int j=0;j<=i;j++){
if (yang[i][j]<10)
s=s+yang[i][j]+" ";
else if (yang[i][j]<100)
s=s+yang[i][j]=" ";
else if (yang[i][j]<1000)
s=s+yang[i][j]+" ";
else
s=s+yang[i][j]+" ";
}
g.drawString(s,250-16*i,50+16*i);
}
}
int[][] yanghui(int i){
int s[][]=new int[i][];
for (int x=0;x<s.length;x++)
s[x]=new int[x+1];
s[0][0]=1;
for (int y=1;y<s.length;y++){
s[y][0]=1;
s[y][s[y].length-1]=1;
for (int z=1;z<s[y-1].length;z++)
s[y][z]=s[y-1][z-1]+s[y-1][z];
}
return s;
}
}
运行结果是:
yanghui.java:28:意外的类型
需要:变量
找到:值 s=s+yang[i][j];
1错误
这个是杨辉三角数值,上面这个我是按照书上的写下来的!自己修改后也是不行!是不是字符型的S不能与数值型的YANG相加呢?那要怎么改
下面这个是我自己写的!
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class yanghui extends Applet implements ActionListener{
Label lb1=new Label("请输入要显示的行数:");
TextField txt1=new TextField(4);
Button btn=new Button("确定");
int n=0;
int[][] yang;
public void init(){
add(lb1);
add(txt1);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
n=Integer.parseInt(txt1.getText());
repaint();
}
int[][] yang(){
for (int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
if(j>1&j<=i-1)
yang[i][j]=yang[i-1][j-1]+yang[i-1][j];
else
yang[i][j]=1;
}
}
return yang;
}
public void paint(Graphics g){
String s="";
for(int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
s=yang[i][j]+" ";
g.drawString(s,250-16*i,50+16*i);
}}
}
}
那位大哥帮我修改一下
import java.awt.event.*;
import java.awt.*;
public class yanghui extends Applet implements ActionListener{
Label lb1=new Label("请输入要显示的行数:");
TextField txt1=new TextField(4);
Button btn=new Button("确定");
int n=0;
int[][] yang;
public void init(){
add(lb1);
add(txt1);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
n=Integer.parseInt(txt1.getText());
yang=yanghui(n);
repaint();
}
public void paint(Graphics g){
for(int i=0;i<n;i++){
String s=" ";
for(int j=0;j<=i;j++){
if (yang[i][j]<10)
s=s+yang[i][j]+" ";
else if (yang[i][j]<100)
s=s+yang[i][j]=" ";
else if (yang[i][j]<1000)
s=s+yang[i][j]+" ";
else
s=s+yang[i][j]+" ";
}
g.drawString(s,250-16*i,50+16*i);
}
}
int[][] yanghui(int i){
int s[][]=new int[i][];
for (int x=0;x<s.length;x++)
s[x]=new int[x+1];
s[0][0]=1;
for (int y=1;y<s.length;y++){
s[y][0]=1;
s[y][s[y].length-1]=1;
for (int z=1;z<s[y-1].length;z++)
s[y][z]=s[y-1][z-1]+s[y-1][z];
}
return s;
}
}
运行结果是:
yanghui.java:28:意外的类型
需要:变量
找到:值 s=s+yang[i][j];
1错误
这个是杨辉三角数值,上面这个我是按照书上的写下来的!自己修改后也是不行!是不是字符型的S不能与数值型的YANG相加呢?那要怎么改
下面这个是我自己写的!
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class yanghui extends Applet implements ActionListener{
Label lb1=new Label("请输入要显示的行数:");
TextField txt1=new TextField(4);
Button btn=new Button("确定");
int n=0;
int[][] yang;
public void init(){
add(lb1);
add(txt1);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
n=Integer.parseInt(txt1.getText());
repaint();
}
int[][] yang(){
for (int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
if(j>1&j<=i-1)
yang[i][j]=yang[i-1][j-1]+yang[i-1][j];
else
yang[i][j]=1;
}
}
return yang;
}
public void paint(Graphics g){
String s="";
for(int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
s=yang[i][j]+" ";
g.drawString(s,250-16*i,50+16*i);
}}
}
}
那位大哥帮我修改一下