gpt4 book ai didi

java - 有没有办法在 createArray() 的子程序 print() 中打印 multArray?

转载 作者:行者123 更新时间:2023-12-01 09:18:55 25 4
gpt4 key购买 nike

我想要两个子程序createArray()print()Print() 将需要 createArray() 中的 multArray 变量,并且我已经编写了程序,以便数组不会在 main 中本地创建。我意识到我可以将 createArray 设置为 createArray(int a, int b) 但我决定不这样做。现在这会回来咬我吗?还是有办法让我在不进行建议的更改的情况下完成此任务?

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) {
}
}
}

最佳答案

您的 createArray 方法返回一个 int[][] 数组,因此您可以执行类似的操作

int[][] multiArray = createArray();//将create array方法的结果存储在multiArray中

现在更改您的 print 方法以接受 int[][] 数组,如下所示

public static void print(int[][] multiArray); //接受 int[][] 数组作为参数的打印方法

当您调用类似这样的打印方法时,将 multiArray 传递给打印方法

print(multiArray) //将 createArray 的早期结果(multiArray)传递给 print 方法。

现在,在 print 方法中,您可以打印 multiArray

关于java - 有没有办法在 createArray() 的子程序 print() 中打印 multArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40300951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com