| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 969 人关注过本帖
标题:数组求和
只看楼主 加入收藏
kingnan1988
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2008-7-13
收藏
 问题点数:0 回复次数:2 
数组求和
编成求解一个3*3矩阵每一行的元素之和,每一列元素之和,对角线元素之和,并输出 要怎么做。。。
谢喽。。。。各位。。。。
急。。。。。。。。。
搜索更多相关主题的帖子: 求和 
2008-11-26 19:50
sxl111
Rank: 1
等 级:新手上路
威 望:1
帖 子:55
专家分:0
注 册:2008-5-7
收藏
得分:0 
用一个二维数组…因为手机上的…只能写这么多了…
2008-11-27 00:30
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
收藏
得分:0 
写了一个,效率不是很好,但是功能实现了
package test;

public class ArraySum {
    
    private int [][] numbers;
    private int [] rowSum;
    private int [] colmSum;
    private int [] diagonalSum;
    
    public ArraySum() {  
        this.numbers = new int[][]{
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
        this.rowSum = new int[3];
        this.colmSum = new int[3];
        this.diagonalSum = new int[2];
    }
    
    void getRowSum() {
        for (int i = 0; i < this.numbers.length; i++) {
            for (int j = 0; j < this.numbers.length; j++) {
                this.rowSum[i] = this.rowSum[i] + this.numbers[i][j];
            }
        }
    }
    
    void getColmSum() {
        for (int i = 0; i < this.numbers.length; i++) {
            for (int j = 0; j < this.numbers.length; j++) {
                this.colmSum[j] = this.colmSum[j] + this.numbers[i][j];
            }
        }
    }
    
    void getDiagonalSum() {
        this.diagonalSum[0] = numbers[0][0] + numbers[1][1] + numbers[2][2];
        this.diagonalSum[1] = numbers[0][2] + numbers[1][1] + numbers[2][0];
    }
    
    void printResult() {
        System.out.println("The sums of the rows are as following:");
        for (int i = 0; i < this.rowSum.length; i++) {
            System.out.print(this.rowSum[i]);
            System.out.print(",");
        }
        System.out.println();
        System.out.println("The sums of the colomns are as following:");
        for (int i = 0; i < this.colmSum.length; i++) {
            System.out.print(this.colmSum[i]);
            System.out.print(",");
        }
        System.out.println();
        
        System.out.println("The sums of the diagonalSum are as following:");
        for (int i = 0; i < this.diagonalSum.length; i++) {
            System.out.print(this.diagonalSum[i]);
            System.out.print(",");
        }
        System.out.println();
    }

    
    public static void main(String[] args) {
        ArraySum application = new ArraySum();
        application.getColmSum();
        application.getRowSum();
        application.getDiagonalSum();
        application.printResult();

    }

}
2008-11-28 15:40
快速回复:数组求和
数据加载中...
 
   



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

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