如何在数组中保存用户输入?

时间:2017-01-05 21:28:31

标签: java arrays

我试图编写该程序要求用户输入要保存在医院数据库中的患者数量,即ArraySize。用户必须输入患者的ID,名字和姓氏,然后患者的ID,名字和名字根据他/她的病情严重程度在数组中编入索引。我的问题是如何将用户的ID输入保存为整个ID阵列的一部分,其限制是由用户输入的?如何索引患者的信息?

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

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 

}

3 个答案:

答案 0 :(得分:1)

如果您不能使用对象,那么您可以使用一个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搜索时,您只需要搜索保存位置的索引,并且与name和lastName相同。希望它能帮到你

答案 1 :(得分:0)

为您提供有关您帖子评论的一些代码。 存储患者信息所需的课程可能如下:

public class Patient
{
  private int id;
  private String name;
  private String lastname;

  public Patient (int id, String name, String lastname)
  {
    // assign values to variables here
  }

  // generate getters and setters
}

然后在您的程序中,您可以使用以下列表:

List<Patient> patients = new ArrayList<Patient>();
Patient p = new Patient (1, "Name", "Lastname");
patients.add(p);

编辑: 由于您只允许使用Arrays,请在主要部分使用:

Patient [] patient = new Patient[2];

Patient one = new Patient(1, "foo", "bar");
Patient two = new Patient(2, "foo2", "bar2");

patient[0] = one;
patient[1] = two;

for (Patient pat : patient) {
    System.out.println("Patient " + pat.getId() + ": " + pat.getName() + " " + pat.getLastname() );
}

患者班保持不变。 输入后,您只需要根据患者的严重程度进行排序,也可以添加到患者类别中。

答案 2 :(得分:0)

使用多个diamentional数组如下

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    if editingStyle == .delete {
         context.delete(tasks[indexPath.row] as NSManagedObject)
        //objects.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)

        //Save the object
        do{
            try (UIApplication.shared.delegate as! AppDelegate).saveContext()
        }
        catch let error{
            print("Cannot Save: Reason: \(error)")
        }

        //Reload your Table Data
        getData()

        //Reload the table view
        tableView.reloadData()

    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}