简体   繁体   English

Java从主函数调用print方法并使用来自另一个单独方法的数据

[英]Java calling a print method from a main function and using data from another separate method

I'm trying to run a program that constructs a 2D array from user input and then prints the array. 我正在尝试运行一个程序,从用户输入构建一个2D数组,然后打印该数组。 The array has to be constructed in one method and print the rows in a separate method. 必须在一种方法中构造数组,并以单独的方法打印行。 I'm only looking to print each row from the array on a new line. 我只想在新行上打印数组中的每一行。 I'm not sure how to pull the array from one method into another. 我不确定如何将数组从一种方法拉到另一种方法。

the code is set up so when the main method is called it will call the "constructor" method to build the array, and then after the array is built it will then take that array and send the data to the print method to print each row. 代码设置,所以当调用main方法时,它将调用“构造函数”方法来构建数组,然后在构建数组之后,它将获取该数组并将数据发送到print方法以打印每一行。 I'm not sure where to proceed from where I'm at in the code, and I still don't understand how to pull data from different methods. 我不知道从哪里开始我在代码中的位置,我仍然不明白如何从不同的方法中提取数据。

package workfiles;

import java.util.*;
import java.util.Scanner;

public class hw2
{
    // Do not modify this method
    public static void main(String[] args)
    {
        try {
            int[][] iArray = enter2DPosArray();
            System.out.println("The original array values:");
            print2DIArray(iArray);

            int[][] tArray = transposition(iArray);
            System.out.println("The transposed array values:");
            print2DIArray(tArray);
        } catch (InputMismatchException exception) {
            System.out.println("The array entry failed. The program will now halt.");
        }
    }

    // A function that prints a 2D integer array to standard output
    // THIS METHOD IS THE ONE THAT IS SUPOSED TO PRINT THE ROWS ON NEW LINES
    public static void print2DIArray(int[][] output)
    {
        int[][] iArray;

        for (int row = 0; row < iArray.length; row++) {
            for (int column = 0; column < iArray[row].length; column++) {
                System.out.print(iArray[row][column] + " ");
            }
            System.out.println();
        }
    }

    // A function that enters a 2D integer array from the user
    // It raises an InputMismatchException if the user enters anything other
    // than positive (> 0) values for the number of rows, the number of
    // columns, or any array entry
    public static int[][] enter2DPosArray() throws InputMismatchException
    {
        int row = 0, col = 0, arow = 0, acol = 0, holder;

        Scanner numScan = new Scanner(System.in);

        while (row <= 0) {
            System.out.print("How many rows (>0) should the array have? ");
            row = numScan.nextInt();
        }

        while (col <= 0) {
            System.out.print("How many columns (>0) should the array have? ");
            col = numScan.nextInt();
        }

        int[][] iArray = new int[row][col];

        while (arow < row) {
            while (acol < col) {
                System.out.println("Enter a positive (> 0) integer value: ");
                holder = numScan.nextInt();
                iArray[arow][acol] = holder;
                acol++;
            }

            if  (acol >= col) {
                acol = 0;
                arow ++;
            }
        }

        //arrayName[i][j]
        numScan.close();
        return iArray;
    }

    public static int[][] transposition(int[][] iArray)
    {
        int r = 0, c = 0;

        int[][] transpose = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                transpose[i][j] = iArray[j][i];
            }
        }
        return transpose;
    }
}
public static void print2DIArray(int[][] output) {    
  int[][] iArray;
  for (int row = 0; row < iArray.length; row++) {

When you call the print2DIArray method, you are attempting to iterate over the empty array you just created. 当您调用print2DIArray方法时,您正在尝试迭代刚刚创建的空数组。 I think you want to iterate over the "output" parameter you passed in. 我想你想迭代你传入的“输出”参数。

You can just change the name in your method like this : 您可以像这样更改方法中的名称:

print2DIArray(int[][] iArray)

So your code should look like this 所以你的代码应该是这样的

//your array can send like an attribute, you can use it like is it
public static void print2DIArray(int[][] iArray) {
    //int[][] iArray you dont need to use another array here

    for (int row = 0; row < iArray.length; row++) {
    //...

I have edited your code like this. 我已经像这样编辑了你的代码。 I hope it would be helpful. 我希望它会有所帮助。

public class Test {

    private static int[][] iArray ;

    public static void main(String[] args) {


    try
    {
        int [][] iArray = enter2DPosArray();
        System.out.println("The original array values:");
        print2DIArray(iArray);
        int [][] tArray = transposition(iArray);
        System.out.println("The transposed array values:");
        print2DIArray(tArray);
    }

    catch (InputMismatchException exception)
    {
        System.out.println("The array entry failed. The program will now halt.");
    }

}

    // A function that prints a 2D integer array to standard output
    // THIS METHOD IS THE ONE THAT IS SUPPOSED TO PRINT THE ROWS ON NEW LINES

    public static void print2DIArray(int[][] output) {
        //int iArray[][] = new int[0][];

        for (int row = 0; row < iArray.length; row++) {
            for (int column = 0; column < iArray[row].length; column++) {
                System.out.print(iArray[row][column] + " ");
            }
            System.out.println();
        }
    }



    // A function that enters a 2D integer array from the user
// It raises an InputMismatchException if the user enters anything other
// than positive (> 0) values for the number of rows, the number of
// columns, or any array entry
    public static int[][] enter2DPosArray() throws InputMismatchException {

        int row=0;
        int col=0;
        int arow=0;
        int acol=0;
        int holder;
        Scanner numScan = new Scanner(System.in);

        while (row<=0){
            System.out.print("How many rows (>0) should the array have? ");
            row = numScan.nextInt();
        }

        while (col<=0){
            System.out.print("How many columns (>0) should the array have? ");
            col = numScan.nextInt();
        }
        iArray = new int[row][col];

        while (arow < row) {

            while (acol < col) {
                System.out.println("Enter a positive (> 0) integer value: ");
                holder = numScan.nextInt();
                iArray[arow][acol] = holder;
                acol++;
            }

            if  (acol >= col) {
                acol = 0;
                arow ++;

            }

        }
        //arrayName[i][j]
        numScan.close();
        return iArray;
    }

    public static int[][] transposition(int [][] iArray) {

        int r=0, c=0;

        int[][] transpose = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                transpose[i][j] = iArray[j][i];
            }
        }
        return transpose;
    }

}

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

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