简体   繁体   English

有什么方法可以从createArray()的子程序print()中打印multArray?

[英]Is there any way to print multArray in sub-program print() from createArray()?

I would like to have two sub-programs createArray() and print() . 我想要两个子程序createArray()print() Print() will require the multArray variable from createArray() and I have written the program so that the array is not created locally in main. Print()将需要createArray()multArray变量,并且我已经编写了程序,以便不在main中本地创建数组。 I realise that I could have set createArray up as createArray(int a, int b) however I decided against it. 我意识到我可以将createArray设置为createArray(int a, int b)但是我决定反对。 Will this come back to bite me now or is there still a way for me to accomplish this without making the suggested change? 现在是否会再次咬我,还是我还有办法在不进行建议的更改的情况下完成此任务?

import java.util.*;
import java.io.*;

public class Array {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String newLine = System.lineSeparator();
        String choiceInput;
        boolean stop = false;
        boolean firstTime = true;


        while (stop == false){

            if (firstTime == true) {
                System.out.println("Welcome To The Multiplications Table Creator!" + newLine + newLine + "Would you like to:" + newLine + newLine + "Create          Print          Exit" + newLine + newLine + "Please enter one of the above in the space below: ");
            }
            else {
                System.out.println("Welcome Back!" + newLine + newLine + "Would you like to:" + newLine + newLine + "Create          Print          Exit" + newLine + newLine + "Please enter one of the above in the space below: ");
            }
            choiceInput = scan.nextLine().toUpperCase();

            if (choiceInput.equals("CREATE")) {
                createArray();
                firstTime = false;

                for (int count = 0; count < 10; count++) {
                    System.out.println(newLine);
                }

            }
            else if (choiceInput.equals("PRINT")) {
                print();
                firstTime = false;
            }
            else if (choiceInput.equals("EXIT")) {
                for (int count = 0; count < 10; count++) {
                    System.out.println(newLine);
                }
                System.out.print("Thank you for using the program!");
                for (int count = 0; count < 2; count++) {
                    System.out.println(newLine);
                }
                stop = true;
            }
            else System.out.println("You did not enter one of the above!");
        }
    }

    public static int[][] createArray() {

        Scanner s = new Scanner(System.in);
        String newLine = System.lineSeparator();
        int a;
        int b;

        System.out.print("How big would you like your multiplication table to be? (A x B)" + newLine + "A: ");
        a = s.nextInt();
        System.out.println(a + " x ");
        b = s.nextInt();

        int[][] multArray = new int[a][b];

        for (int countA = 1; countA <= a; countA++) { 
            for (int countB = 1; countB <= b; countB++) {
                multArray[countA - 1][countB - 1] = countA * countB;
            }

        }
        System.out.print("Creating .");
        delay(1000);
        System.out.print(" .");
        delay(1000);
        System.out.print(" .");
        delay(1000);
        System.out.println(newLine + "Done.");
        return multArray;

    }

    public static void print() {
        **//This is where I need to print multArray created above is it possible?**
    }

    public static void delay(int millis) {
        try {
            Thread.sleep(millis);
        } 
        catch (InterruptedException exp) {  
        }
    }
}

Your createArray method returns an int[][] array, so you can do something like this 您的createArray方法返回一个int[][]数组,因此您可以执行以下操作

int[][] multiArray = createArray(); // storing the outcome of create array method in multiArray //将创建数组方法的结果存储在multiArray中

Now change your print method to accept an int[][] array, something like this 现在更改您的print方法以接受一个int[][]数组,像这样

public static void print(int[][] multiArray); // print method which accepts an int[][] array as parameter //接受int [] []数组作为参数的print方法

pass multiArray to print method when you call print method something like this 当您调用打印方法时,将multiArray传递给打印方法

print(multiArray) // passing the earlier outcome of createArray which is multiArray to print method. print(multiArray) //将createArray的早期结果 print(multiArray) 传递给print方法。

Now inside print method you can print multiArray . 现在在print方法中,您可以打印multiArray

You want to iterate through your y- and x-axis. 您想遍历y轴和x轴。 This example should print all row elements for each column. 本示例应为每一列打印所有行元素。 Therefore the first for-loop iterates through your rows and the second through every element in the individual row. 因此,第一个for循环遍历您的行,第二个遍历单个行中的每个元素。

public static void printArray(int[][] multArray) { 
    for(int i = 0; i < multArray.lenght(); i++)
       {
          for(int j = 0; j < multArray[i].lenght(); j++)
          {
             System.out.printf("%5d ", multArray[i][j]);
          }
          System.out.println();
       }

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

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