如何将用户输入保存在数组中? [英] How do you save user input inside an array?

查看:79
本文介绍了如何将用户输入保存在数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,要求用户输入要保存在医院数据库(即 ArraySize )中的患者数.用户必须输入患者的ID,名字和姓氏,然后根据其状况的严重程度在数组中索引患者的ID,名字和姓氏.我的问题是如何将用户的ID输入保存为整个ID数组的一部分,该ID数组的限制由用户输入?我如何索引患者的信息?

I'm trying to write a that program asks the user to enter the number of patients to be saved in a hospital's database, which is ArraySize . The user must enter the patient's ID, first and last name, and then the patient's ID, first and last names are indexed in the array according to the severity of his/her condition. My question is how do I save the user's ID input to be a part of the entire ID array, whose limit is as entered by the user? How do I index the patient's information?

到目前为止,这是我的代码:

Here's my code so far:

System.out.print("\n > How many patients allowed for day 25-12-2016 : ");
        int ArraySize = input.nextInt();

 System.out.print("\n       > Enter patient ID: ");
                int ID[] = new int[ArraySize];

                for (int i = 0; i < ID.length; i++) {
                    ID[i] = input.nextInt();
                }


   System.out.print("\n         > Enter patient First Name: ");
            String first[] = new String[ArraySize];
            for (int i = 0; i < first.length; i++) {
                first[i] = reader.nextLine();
            }
            System.out.print("\n        > Enter patient last Name: ");
            String last[] = new String[ArraySize];
            for (int i = 0; i < last.length; i++) {
                last[i] = reader.nextLine();
            }

 System.out.print("\n _______________________________________________\n"
                + "|                                               |\n"
                + "|                  Case Type                    |\n"
                + "|_______________________________________________|\n"
                + " \n"
                + " _______________________________________________\n"
                + "|                        |\n"
                + "| 1: Enter 1 for Accident Injury                | \n"
                + "| 2: Enter 2 for Fire Injury                    | \n"
                + "| 3: Enter 3 for Electricity Sho                | \n"
                + "| 4: Enter 4 for Heart Attack                   | \n"
                + "| 5: Enter 5 for Unconscious                    | \n"
                + "| 6: Enter 6 for Otherwise                      | \n"
                + "|_______________________________________________|\n"
                + " > Please enter your choice: \n"
                + " ");
int choice = input.nextInt();

if (int==1)

{
// index patient at 0 

}

推荐答案

好吧,如果您不能使用对象,那么您可以使用一种适用于一切的

Well if you cannot use Objects then you use one For Everything

   for (int i = 0; i < ID.length; i++) {
         System.out.print("\n       > Enter patient ID: ");
         ID[i] = input.nextInt();

        System.out.print("\n         > Enter patient First Name: ");
        first[i] = reader.nextLine();

        System.out.print("\n        > Enter patient last Name: ");
        last[i] = reader.nextLine();
                    }

现在,当有人通过ID搜索时,您只需要搜索保存位置的索引,该索引与名称和姓氏相同.希望对您有帮助

Now when someone search by ID you only need the search the index of where is it saved and is the same as the name and lastName. Hope if it help you

这篇关于如何将用户输入保存在数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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