Ich hätte gerne zwei Unterprogramme createArray()
und print()
. Print()
erfordert die multArray
Variable von createArray()
und ich habe das Programm geschrieben, so dass das Array nicht lokal in Main erstellt wird. Ich merke, dass ich createArray
als createArray(int a, int b)
setzen konnte, aber ich entschied mich dagegen. Wird das jetzt zurückkommen, um mich zu beißen, oder gibt es noch einen Weg, dies zu erreichen, ohne die vorgeschlagene Änderung zu machen?Gibt es eine Möglichkeit, multArray im Unterprogramm print() von createArray() zu drucken?
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) {
}
}
}
sidnote 1: Definieren Sie einen globalen Scanner und verwenden Sie ihn erneut. Sie müssen nicht mehrere davon definieren. sidenote 2: 'if (! stop)' und 'if (firstTime)' liest sich besser als der Vergleich mit einem bool-Wert in meinen Augen. – SomeJavaGuy
Sie haben das Ergebnis der Funktion 'createArray()' nicht gespeichert, daher können Sie 'print()' nicht richtig verwenden. Sie müssen es als ein privates Mitglied speichern und dann in 'print()' darauf zugreifen oder es als Parameter an 'print()' übergeben. – Ghost93
Ist das 'return multArray;' in diesem Fall nicht ausreichend? In Bezug auf Code, was würde ich von mir verlangen? –