简体   繁体   English

打印二维阵列的方法

[英]Method for Printing 2D Arrays

I'm trying to create a method to print two-dimensional arrays. 我正在尝试创建一种打印二维数组的方法。 I did some research and tried to apply the answers I found but it's not working with my code. 我进行了一些研究,并尝试应用发现的答案,但不适用于我的代码。

Basically I'd like to call this method 基本上我想调用此方法

static void showMatrix(int[][] numbers){
    for (int i = 0; i < numbers.length; i++)
    {
        for (int j = 0; j < numbers[i].length; j++)
        {
            System.out.print(numbers[i][j] + " ");
        }
        System.out.println();
    }
}

here: 这里:

public static int[][] matrixCreate()    
{
    Scanner entry = new Scanner(System.in);

    int matrix[][] = new int[2][2];

    for (int i = 0; i < matrix.length; i++){
        for (int j = 0; j < matrix[i].length; j++){
            System.out.print("[" + i + ", " + j + "]: ");
            matrix[i][j] = entry.nextInt(); 
        }
    }

    //Print New Matrix      
    for (int i = 0; i < matrix.length; i++)
    {
        for (int j = 0; j < matrix[i].length; j++)
        {
            System.out.print(matrix[i][j] + " ");
        }           
        System.out.println();
    }       

    return showMatrix(matrix);
}

I tried to change the return types and argument but It didn't work. 我试图更改返回类型和参数,但这没有用。 Do you guys have any tips for me? 你们对我有什么提示吗?

Try at the end: 最后尝试:

showMatrix(matrix);
return matrix;

The problem being that showMatrix() doesn't return anything, so it cannot be "returned", but all you wanted was to return the matrix itself anyway. 问题是showMatrix()不返回任何东西,因此不能“返回”,但是您想要的只是返回矩阵本身。

And welcome to SO, let me know if this isn't detailed enough, I could look at it more but this just looked like something that definitely wouldn't work as is. 欢迎来到SO,让我知道它是否还不够详细,我可以多看一点,但这看起来确实无法正常工作。

To make your life simpler in the future--you might consider downloading eclipse or intellij... for a new java user they will be invaluable for helping you with syntax problems--a real life saver. 为了使您的生活更简单-您可以考虑为新的Java用户下载eclipse或intellij ...,它们对于帮助您解决语法问题将是无价之宝-真正的救星。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM