如何使用私有静态void方法访问数组 [英] How to access an array using a private static void method

查看:88
本文介绍了如何使用私有静态void方法访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a beginner trying to program a train booking system which has 8 seats and requires the user to input various letters in order to execute a method. I am struggling to create a method through a private static void which when the user enters ‘E’ It shows all the empty seats through an array.

下面是代码到目前为止,我目前为止已经完成了:

Here is the code that I have currently done so far:

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 放入占用的索引(席位)。仅出于演示目的,我保留了席位。 0、3和7,方法是将 1 放入其中。在 showEmptySeats 里面,我检查了 train 数组中是否有 0 (表示为空)。

Note that uninitialized indices in an int array will have a value of 0. You can put 1 into the indices (seats) which get occupied. Just for the demo, I have reserved seat no. 0, 3 and 7 by putting 1 into them. Inside showEmptySeats, I have checked the train array for the indices which have 0 (means empty).

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
--------------------------------------------------

我希望,您应该能够从这里开始。如果您需要任何其他帮助,请随时发表评论。

I hope, you should be able to proceed from here. Feel free to comment in case you need any further help.

这篇关于如何使用私有静态void方法访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆