当前位置: 代码迷 >> Java相关 >> 求助,简易JAVA程序
  详细解决方案

求助,简易JAVA程序

热度:308   发布时间:2008-08-22 14:14:32.0
求助,简易JAVA程序
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);
            }}
        }
    }      
    
    那位大哥帮我修改一下
搜索更多相关的解决方案: java  

----------------解决方案--------------------------------------------------------
难道没人吗
----------------解决方案--------------------------------------------------------
前面的代码有个地方敲错哒吧, else if (yang[i][j]<100)
                    s=s+yang[i][j]="    ";
把"="改成"+"
----------------解决方案--------------------------------------------------------
  相关解决方案