下面的代码询问用户他们的巴士路线以及一些相关信息。程序中反复出现错误。
我无法在第二种方法中使用该数组。我尝试将数组设为全局变量,但随后发生了错误。如何从 busRoutes()
访问数组变量decisionMaking()
中的方法.
如果唯一的方法是将其设为全局变量,您能告诉我如何防止错误发生吗?
public class question {
public static void main(String[] args) {
busRoutes();
decisionMaking();
System.exit(0);
}
public static void busRoutes() {
Scanner sc = new Scanner(System.in);
System.out.println("How many bus routes are there");
int routes = sc.nextInt();
String[][] array = new String[routes][4];
for (int i = 0; i < routes ; i++) {
System.out.println("Please enter the bus route");
System.out.println("What is the bus route");
array[i][0] = sc.nextLine();
System.out.println("Start Location");
array[i][1] = sc.nextLine();
System.out.println("End Location");
array[i][2] = sc.nextLine();
System.out.println("Who is the driver for bus route " + array[i][0]);
array[i][3] = sc.nextLine();
}
}
public static void decisionMaking() {
System.out.println("Do you want to 1) Print all bus routes 2) Start a new day 3) Quit ? ");
int decision = sc.nextInt();
if (decision == 1) {
for (int i = 0; i < array.length; i++) {
System.out.println(Arrays.toString(array[i]));
}
}
else if (decision == 3) {
return;
}
}
return;
}
} }
最佳答案
您的问题是 sc
和 array
对 decisionMaking
不可见,您可以将其设为全局变量或将其传递给该方法。
您不应该将所有内容设为全局,但如果未修改,则将其作为参数传递。
public static void main(String[] args) {
busRoutes();
System.exit(0);
}
从主方法中删除了decisionMaking
。并向其中添加了 2 个参数。
public static void DecisionMaking(Scanner sc, String[][] array)
现在我们从busRoutes
传递它
public static void busRoutes() {
Scanner sc = new Scanner(System.in);
System.out.println("How many bus routes are there");
int routes = sc.nextInt();
String[][] array = new String[routes][4];
for (int i = 0; i < routes; i++) {
System.out.println("Please enter the bus route");
System.out.println("What is the bus route");
array[i][0] = sc.nextLine();
System.out.println("Start Location");
array[i][1] = sc.nextLine();
System.out.println("End Location");
array[i][2] = sc.nextLine();
System.out.println("Who is the driver for bus route " + array[i][0]);
array[i][3] = sc.nextLine();
}
decisionMaking(sc, array);
}
这是完整的代码。
import java.util.Arrays;
import java.util.Scanner;
public class question {
public static void main(String[] args) {
busRoutes();
System.exit(0);
}
public static void busRoutes() {
Scanner sc = new Scanner(System.in);
System.out.println("How many bus routes are there");
int routes = sc.nextInt();
String[][] array = new String[routes][4];
for (int i = 0; i < routes; i++) {
System.out.println("Please enter the bus route");
System.out.println("What is the bus route");
array[i][0] = sc.nextLine();
System.out.println("Start Location");
array[i][1] = sc.nextLine();
System.out.println("End Location");
array[i][2] = sc.nextLine();
System.out.println("Who is the driver for bus route " + array[i][0]);
array[i][3] = sc.nextLine();
}
decisionMaking(sc, array);
}
public static void decisionMaking(Scanner sc, String[][] array) {
System.out.println("Do you want to 1) Print all bus routes 2) Start a new day 3) Quit ? ");
int decision = sc.nextInt();
if (decision == 1) {
for (int i = 0; i < array.length; i++) {
System.out.println(Arrays.toString(array[i]));
}
} else if (decision == 3) {
}
}
}
关于java - 如何在 Java 中的一个方法中使用另一个方法中的数组而不使其成为全局数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53735106/