我想有两个子程序createArray()
和print()
。 Print()
将需要multArray
变量createArray()
我写了程序,以便该数组不是在本地创建的主要。我意识到我可以将createArray
设置为createArray(int a, int b)
但是我决定反对它。这会回来咬我现在还是还有我的方式来完成这个没有做出建议的变化?有没有办法从createArray()在子程序print()中打印multArray?
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) {
}
}
}
notenote 1:定义一个全局扫描程序并重用它,不需要定义每个扫描程序的倍数。旁注2:“if(!stop)”和“if(firstTime)”比我眼中的bool值更好。 – SomeJavaGuy
您没有存储函数'createArray()'的结果,因此您无法正确使用'print()'。 您需要将它作为私有成员存储,然后在'print()'中访问它,或者将它作为参数传递给'print()'。 – Ghost93
在这种情况下,'return multArray'是不够的吗?在代码方面,这需要我做什么? –