当前位置: 代码迷 >> Java相关 >> 新手,求改下,看不出来那里错了
  详细解决方案

新手,求改下,看不出来那里错了

热度:343   发布时间:2012-03-18 14:25:26.0
新手,求改下,看不出来那里错了
编写4阶蛇形矩阵
1 3   4 10
2 5   9 11
6 8  12 15
7 13 14 16
思路是创个4*4数组,然后以这矩阵来个每位数赋值,但我真不知道那里赋值错了,程序能运行,但是这赋值有问题。因为我试了这t有问题;那赋值绝对出错了。
要是语法有问题,求大侠也改下。
但是,请不要把主体改太多谢谢。(因为我还想改成随便输入n,然后在打印出来蛇形矩阵,这是之后的事了)
程序代码:

public class SnakeTypeMatrix {
   
    public void print(int[][] a,int n){
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                System.out.print(a[i][j]);
            }
            System.out.println();
        }
    }
   
    public int[][] Assignment(int n){
        int[][] a = new int[n][n];
        int i = 0,j = 0,t = 1;
        for(i = 0;i < 2 * n - 1;i++){
            if(i < n){
                for(j = 0;j < i;j++){
                    if(i % 2 == 0){
                        a[j][i - j] = t;
                    }
                    else{
                        a[i - j][j] = t;
                    }
                    t = t + 1;
                }
            }
            else{
                for(j = 3;j > i - n;j--){
                    if(i % 2 == 0){
                        a[i - j][j] = t;
                    }
                    else{
                        a[j][i - j] = t;
                    }
                    t = t + 1;
                }
            }
        }
        System.out.println(t);
        return a;
    }
   
    public static void main(String[] args){
        SnakeTypeMatrix q = new SnakeTypeMatrix();
        int n = 4;
        int[][] a = new int[n][n];
        a = q.Assignment(n);
        q.print(a, n);
    }
   
}
搜索更多相关的解决方案: 矩阵  

----------------解决方案--------------------------------------------------------
求回帖。。。。
----------------解决方案--------------------------------------------------------
程序代码:
public int[][] Assignment(int n) {
        int[][] a = new int[n][n];
        int i = 0, j = 0, t = 1;
        for (i = 0; i < 2 * n - 1; i++) {
            if (i < n) {
                for (j = 0; j <= i; j++) {
                    if (i % 2 == 0) {
                        a[j][i - j] = t;
                    } else {
                        a[i - j][j] = t;
                    }
                    t = t + 1;
                }
            } else {
                for (j = 3; j > i - n; j--) {
                    if (i % 2 == 0) {
                        a[i - j][j] = t;
                    } else {
                        a[j][i - j] = t;
                    }
                    t = t + 1;
                }
            }
        }
        //System.out.println(t);
        return a;
    }
for (j = 0; j < i; j++) {
改成for (j = 0; j <= i; j++) {
自己把数值带入验算下就能发现问题了嘛
----------------解决方案--------------------------------------------------------
程序代码:
/**编写4阶蛇形矩阵
*1 3   4 10
*2 5   9 11
*6 8  12 15
*7 13 14 16
*/

package smooth.bccn01;
public class Bccn01 {
  
    public void print(int[][] a,int n){
         for(int i = 0;i < n;i++){
             for(int j = 0;j < n;j++){
                 System.out.print(a[i][j]+"\t");
             }
             System.out.println();
         }
     }
   
    public int[][] Assignment(int n){
         int[][] a = new int[n][n];
         int row = 0,col = 0,t = 1;
         boolean isCol = true;
      
         for(int i = 0;i < 2 * n - 1;i++){
             row = i;
             while(row >= ((i < n) ? 0 : i - n + 1)){
                 if(row > (n - 1))
                     row = n - 1;
                 col = i - row;
                 if(isCol)
                     a[col][row] = t;
                 else
                     a[row][col] = t;
                 t ++;
                 row --;
             }
             isCol = !isCol;
     }
         return a;
    }
   
    public static void main(String[] args){
         Bccn01 q = new Bccn01();
         int n = 4;
         int[][] a = new int[n][n];
         a = q.Assignment(n);
         q.print(a, n);
     }
   
}

----------------解决方案--------------------------------------------------------
谢谢回复,虽然我今天重看程序的时候已经发现错误了,但我还是想谢谢一下
----------------解决方案--------------------------------------------------------
  相关解决方案