创建平方矩阵

时间:2019-06-26 07:10:19

标签: java matrix

你好,有人可以帮我吗,我正在尝试用以下格式在Java中做一个方阵:

1 | 6 | 7 | 12
2 | 5 | 8 | 11
3 | 4 | 9 | 10

我想要的是代码生成用户指定大小的表,并使用上述给定格式打印它。

我以为我已经完成了,但是当我输入一个奇数作为COLUMN时,它将总是添加另一列。例如,
**输入:**我输入了3作为no. of columns
**输出:**它将打印四(4)列。

这是我的代码(用Java编写):

System.out.print("Enter Number(s) of ROW: ");

int numRow = in.nextInt();
System.out.print("Enter Number(s) of COLUMN: ");

int numCol = in.nextInt();
int [][] Table = new int[numRow][numCol];
int ctr=0;

for(int row=0; row<numRow; row++) 
{
        for(int col=0; col<numCol; col++)
        {
                Table[row][col]= (col*numRow)+row+1;
                System.out.print(Table[row][col]+"\t");

                for(int i=col+1; i<=numRow; i++)
                {   
                    ctr=(numRow-1)-row;
                    Table[row][col]= (i*numRow)+ctr+1;
                    System.out.print(Table[row][col]+"\t"); 
                    i=numRow;
                }
                col++;               
        }
        System.out.println();
}

4 个答案:

答案 0 :(得分:1)

使用此代码:

import java.util.Scanner;

public class Teas {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
    System.out.print("Enter Number(s) of ROW: ");

    int numRow = in.nextInt();

    System.out.print("Enter Number(s) of COLUMN: ");

    int numCol = in.nextInt();

    int [][] Table = new int[numRow][numCol];

    int ctr=0;

    for(int row=0; row<numRow; row++) 
    {
            for(int col=0; col<numCol; col++)
            {
                if(col%2 == 0){Table[row][col] = (col * numRow +1 )+ row;}
                else{Table[row][col] = (numRow * (col + 1))- row;}
            }
    }
        for(int row=0; row<numRow; row++) 
    {
            for(int col=0; col<numCol; col++)
            {
                System.out.print(Table[row][col] +"|");
            }
            System.out.println("");
    }

    }

    }

答案 1 :(得分:0)

作为其他答案,您的问题来自第3个for循环。但是更清楚地说,我将您的工作分为两部分,它们正在构建和显示。

首先构建矩阵。

int value = 1;
for(int col=0; col<numCol; col++) 
{
        if(col%1==1){ 
            for(int row=0; row<numRow; row++)
            {
                Table[row][col] = value+numRow-row;
            } 
            value = table[0][col]+1;
        }
        else{
            for(int row=0; row<numRow; row++)
            {
                Table[row][col] = value++;
            } 
        }  
}

然后将矩阵显示为所需的格式。

for(int i = 0;i<numRow;i++){
    for(int j=0;j<numCol;j++)
    System.out.print(Table[i][j]);
    if(j==numCol-1) System.out.print("\n");
    else System.out.print("|");
}

答案 2 :(得分:0)

在第3个循环中使用def mapp(h): h=h.reshape((4,2)) hnew=np.zeros((4,2),dtype = np.float32) add=h.sum(axis=1) hnew[0]=h[np.argmin(add)] hnew[3]=h[np.argmax(add)] #put 3 NOT 2 diff=np.diff(h,axis = 1) hnew[1]=h[np.argmin(diff)] hnew[2]=h[np.argmax(diff)] #put 2 NOT 3 return hnew 代替<

<=

答案 3 :(得分:0)

问题似乎来自于此: for(int i=col+1; i<=numRow; i++)
您正在以numRow等于或小于等于的方式执行循环,这意味着当您使用指令设置单元格值时:Table[row][col]= (i*numRow)+ctr+1 如果最大列数为3,它将填充Table [row] [3],因为数组索引从0开始,它将添加第四列。