| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 893 人关注过本帖
标题:求教杨辉三角编程错误
只看楼主 加入收藏
SD7436
Rank: 2
等 级:论坛游民
帖 子:48
专家分:25
注 册:2008-8-12
结帖率:100%
收藏
 问题点数:0 回复次数:2 
求教杨辉三角编程错误
import *;
public class O
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
        int temp,i;
        String c;
        System.out.print("请输入所需行数:");
        c=keyin.readLine();
        temp=Integer.parseInt(c);
        if(temp<=0)
        System.out.println("您输入的数没意义!");
        else if(temp==1)
        {
            int b[]=new int[1];
            b[0]=1;
            System.out.println(b[0]);
        }
        else if(temp==2)
        {
            System.out.println("1"+'\n'+"1"+"1");
            
        }
        else
        {
            int a[][]=new int[temp][];
            for(i=1;i<=temp;i++)
            {
                a[i-1]=new int[i];
            }
                a[0][0]=a[1][0]=a[1][1]=a[temp-1][0]=1;
                int p,q;
                for(p=2;p<=temp;p++)
                {
                    for(q=1;q<p;q++)
                    a[p][q]=a[p-1][q-1]+a[p-1][q];     //此行出现错误!
                    
                }
            System.out.println("1"+'\n'+"1"+"1");
            for(p=2;p<=temp;p++)
            {
                for(q=0;q<p;q++)
                System.out.print(a[p][q]);
                System.out.println("1");
            }
        }
    }
}
编译能通过,但输入3后出现错误
搜索更多相关主题的帖子: 杨辉三角 
2008-10-14 16:02
pls00001
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-9-29
收藏
得分:0 
我做了一个,你看看
package test;

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        int temp;
        temp = input.nextInt();

        int[][] a = new int[temp][temp];
        for (int i = 0; i < temp; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0 || i == j) {
                    a[i][j] = 1;
                }
                if (i < temp - 1) {
                    a[i + 1][j + 1] = a[i][j] + a[i][j + 1];
                }
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}
2008-10-14 20:02
SD7436
Rank: 2
等 级:论坛游民
帖 子:48
专家分:25
注 册:2008-8-12
收藏
得分:0 
多谢二楼的,但最好还请大家帮忙解释下我为什么那步会出现错误,麻烦了
2008-10-15 16:34
快速回复:求教杨辉三角编程错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021243 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved