如何將數組傳遞給方法以便我可以在另一個方法中使用switch
語句中的方法?java中的方法和數組
package assignment8exam;
import java.util.Scanner;
/**
*
* @author Anders
*/
public class Course {
Scanner sc = new Scanner(System.in);
public void MainMenu() {
System.out.println("Enter data about a studen, start by entering how many");
int numbers = sc.nextInt();// amount of student
for (int i = 0; i < numbers; i++) {
System.out.println("Enter name of student");
Scanner name = new Scanner(System.in);
String names = name.nextLine();
String studentNames[] = new String[numbers];
}
for (int j = 0; j < numbers; j++) {
System.out.println("Enter grade of student");
Scanner gradeSc = new Scanner(System.in);
int grade = gradeSc.nextInt();
int studentGrade[] = new int[numbers];
}
System.out.println("What do you want");
System.out.println("Exit application 1");
System.out.println("Print out all names of the students 2");
System.out.println("Print out all the grades of the students 3");
System.out.println("Print out pairs consisting of 「namegrade 4");
Scanner choice = new Scanner(System.in);
int order = choice.nextInt();
switch (order) {
case 1:
System.exit(1);
case 2:
PrintOutNames(numbers); /* so i can use this one with studentName array*/
}
}
public void PrintOutNames(int numbers) {
for (int i = 0; i < numbers; i++) {
System.out.println(studentNames[i].length); // How do i activate the array studentNames??
}
}
}
只要改變你的方法簽名'PrintOutNames(INT數字,字符串[] studentNames)'然後通過在陣列中,當你調用該方法。 –
您的'studentNames'數組只存在於for循環中。與'studentGrade'數組一樣。改變這將有所幫助。你的代碼也有一個[切換開關](http://stackoverflow.com/questions/188461/switch-statement-fallthrough-should-it-be-allowed)問題。 – Obicere