简体   繁体   中英

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

I would like to have two sub-programs createArray() and 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. I realise that I could have set createArray up as createArray(int a, int b) however I decided against it. 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

int[][] multiArray = createArray(); // storing the outcome of create array method in multiArray

Now change your print method to accept an int[][] array, something like this

public static void print(int[][] multiArray); // print method which accepts an int[][] array as parameter

pass multiArray to print method when you call print method something like this

print(multiArray) // passing the earlier outcome of createArray which is multiArray to print method.

Now inside print method you can print multiArray .

You want to iterate through your y- and x-axis. 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.

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();
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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