如何使用私有静态void方法访问数组



我是一个初学者,正在尝试对一个有8个座位的火车预订系统进行编程,该系统需要用户输入各种字母才能执行一个方法。我正在努力通过一个私有静态void创建一个方法,当用户输入"E"时,它会通过一个数组显示所有空座位。

以下是我目前所做的代码:

package trainbookingsystem;
import java.util.Scanner;
public class Trainbookingsystem {           
static final int NUMBER_OF_ROOMS = 8;
public static void main(String[] args) {
int [] Train = new int [NUMBER_OF_ROOMS];
//Display an welcome and introduction to program
//repeat
Scanner in = new Scanner(System.in);
char choice;
do
{
//display a menu
displayMenu();
//read a choice
System.out.println("--------------------------------------------------");
System.out.println("Enter a letter to select an option or (Q) to exit");
System.out.println("--------------------------------------------------");
choice = in.next().toUpperCase().charAt(0);
//process that choice
switch(choice)
{
case 'Q' :System.out.println("");
break;
case 'E' : System.out.println("You chose empty room");
showEmptySeats(Train);
break;
default: System.out.println("You enetered an invalid choice");
}
//until the user presses 'Q', while choice is not 'Q'
} while (choice != 'Q');
//Exit anf Farewell
System.out.println("Thank you for using our train booking system!");
}

private static void displayMenu() {
System.out.println("|Welcome to the Train booking system|");
System.out.println("*Use the following guide to naviagte through the program*");
System.out.println("---------------------------------------------------------");
System.out.println("| A |Add customer to seat");
System.out.println("| V |View all seats");
System.out.println("| E |Display Empty seats");
System.out.println("| D |Delete customer from seat");
System.out.println("| F |Find the seat for a given customers name");
System.out.println("| S |Store program data in to file");
System.out.println("| L |Load program data in to file");
System.out.println("| O | View seats Ordered alphabetically by name");
}
private static void showEmptySeats(int[] someArray  ) {
//Go through train seats array
// if a seat is empty
int a = someArray[4];
//dsiplay it

}    
}
请注意,int数组中未初始化的索引的值为0。您可以将1放入已占用的索引(座位(中。只是为了演示,我通过将1放入0、3和7号座位来预订。在showEmptySeats中,我检查了train数组中具有0(表示为空(的索引。
import java.util.Scanner;    
public class Trainbookingsystem {
static final int NUMBER_OF_ROOMS = 8;
public static void main(String[] args) {
int [] train = new int [NUMBER_OF_ROOMS];
train[0]=1;
train[3]=1;
train[7]=1;
//Display an welcome and introduction to program
//repeat
Scanner in = new Scanner(System.in);
char choice;
do
{
//display a menu
displayMenu();
//read a choice
System.out.println("--------------------------------------------------");
System.out.println("Enter a letter to select an option or (Q) to exit");
System.out.println("--------------------------------------------------");
choice = in.next().toUpperCase().charAt(0);
//process that choice
switch(choice)
{
case 'Q':
System.out.println("");
break;
case 'E':
System.out.println("You chose empty room");
showEmptySeats(train);
break;
default:
System.out.println("You enetered an invalid choice");
}
//until the user presses 'Q', while choice is not 'Q'
} while (choice != 'Q');
//Exit anf Farewell
System.out.println("Thank you for using our train booking system!");
}
private static void displayMenu() {
System.out.println("|Welcome to the Train booking system|");
System.out.println("*Use the following guide to naviagte through the program*");
System.out.println("---------------------------------------------------------");
System.out.println("| A |Add customer to seat");
System.out.println("| V |View all seats");
System.out.println("| E |Display Empty seats");
System.out.println("| D |Delete customer from seat");
System.out.println("| F |Find the seat for a given customers name");
System.out.println("| S |Store program data in to file");
System.out.println("| L |Load program data in to file");
System.out.println("| O | View seats Ordered alphabetically by name");
}
private static void showEmptySeats(int[] train) {
for(int i = 0;i<train.length;i++) {
if(train[i]==0) {
System.out.println("Seat no. "+i+" is empty");
}
}
}
}

样本运行:

|Welcome to the Train booking system|
*Use the following guide to naviagte through the program*
---------------------------------------------------------
| A |Add customer to seat
| V |View all seats
| E |Display Empty seats
| D |Delete customer from seat
| F |Find the seat for a given customers name
| S |Store program data in to file
| L |Load program data in to file
| O | View seats Ordered alphabetically by name
--------------------------------------------------
Enter a letter to select an option or (Q) to exit
--------------------------------------------------
E
You chose empty room
Seat no. 1 is empty
Seat no. 2 is empty
Seat no. 4 is empty
Seat no. 5 is empty
Seat no. 6 is empty
|Welcome to the Train booking system|
*Use the following guide to naviagte through the program*
---------------------------------------------------------
| A |Add customer to seat
| V |View all seats
| E |Display Empty seats
| D |Delete customer from seat
| F |Find the seat for a given customers name
| S |Store program data in to file
| L |Load program data in to file
| O | View seats Ordered alphabetically by name
--------------------------------------------------
Enter a letter to select an option or (Q) to exit
--------------------------------------------------

我希望你能从这里开始。如果您需要任何进一步的帮助,请随时发表评论。

最新更新