统计信息

  • 访问数:8712
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q20

2008-04-17 21:00:36

天气: 晴朗 心情: 高兴

/*filename:P6Q20.java
 *Name:Wang Chaoran
 *Description:
(Summing the major diagonal in a matrix) Write a method that sums all the integers in the major diagonal in a matrix of integers.
 Use {{1, 2, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13}, {14, 15, 16, 17}} to test the method.
*/
class P6Q20{
  public static void main(String[] args){
    int[][] test = {{1, 2, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13}, {14, 15, 16, 17}};
    System.out.println("The sum of ");
    print(test);
    System.out.println("diagnally is "+sumDiagonal(test));
  }
  static int sumDiagonal(int[][] test){
  int sum = 0;
  for(int i=0;i<test.length;i++){
    sum+=test[i][i];
    sum+=test[i][test.length-i-1];

  }
  return sum;
  }
   static void print(int[][] test){
    System.out.print("{");
    for(int i=0;i<test.length;i++){
    System.out.print("{");
    for(int j=0;j<test[i].length;j++){
      if(j==test[i].length-1)
      System.out.print(test[i][j]);
      else
    System.out.print(test[i][j]+",");
    }
    if(i==test.length-1)
       System.out.print("}");
      else
    System.out.print("},");
    }
    System.out.print("}\n");
  }
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd