【问题标题】:check if the user's input is valid or invalid检查用户的输入是否有效
【发布时间】:2018-11-30 11:59:24
【问题描述】:

我写了一个添加约会的方法。用户输入输入内容(日期、时间、医生姓名、科室名称、患者姓名),如果用户输入错误的医生姓名或患者,则会显示输入无效的消息,直到用户输入有效输入为止。

我尝试了此代码,但是当我输入错误名称时,它不会显示无效消息。我试过用contains,还是一样。

public void add_appointment(ArrayList<appointment> appointments,
                            ArrayList<doctor> doctors,
                            ArrayList<section> sections,
                            ArrayList<Patient> PatientList,
                            ArrayList<String> dayArray,
                            ArrayList<String> timeArray) {
    Scanner input = new Scanner(System.in);
    System.out.println("==== Book appointment ====");
    System.out.println("* to book an appointment choose a doctor, section, time and day from below :");
    System.out.print("Doctors :  ");

    for (int r = 0; r < doctors.size(); r++) {
        System.out.print(doctors.get(r).getName() + ", ");
    }

    System.out.println("\n Sections : { Dermal , Dental } ");
    System.out.println("Times : from { 8AM to 4PM } ");
    System.out.println("Days : from { Sunday to Thursday } ");
    System.out.println(" ");

    System.out.println("please enter day :");
    String a1 = input.nextLine();
    if (!dayArray.contains(a1)) {
        System.out.println("invalid day , please enter another day : ");
        a1 = input.nextLine();
    }

    System.out.println("please enter time : ");
    String a2 = input.nextLine();
    if (!timeArray.contains(a2)) {
        System.out.println("invalid time , please enter another time : ");
        a2 = input.nextLine();
    }

    System.out.println("please enter the doctor name : ");
    String a3 = input.nextLine();
    for (int w = 0; w < doctors.size(); ++w) {
        if (doctors.contains(a3)) {
            System.out.println("invalid doctor , the doctor doesn't exists. please enter dr.name : ");
            a3 = input.nextLine();
        }
    }

    System.out.println("please enter the section : ");
    String a4 = input.nextLine();
    for (int e = 0; e < PatientList.size(); ++e) {
        if (sections.contains(a4)) {
            System.out.println("invalid section , the section doesn't exists. please enter section name : ");
            a4 = input.nextLine();
        }
    }

    System.out.println("please enter your name : ");
    String a5 = input.nextLine();
    for (int f = 0; f < PatientList.size(); ++f) {
        if (PatientList.contains(a5)) {
            System.out.println("invalid patient , the patient doesn't exists. please enter name again : ");
            a5 = input.nextLine();
        }
    }

    System.out.println("please assign number to your appointment : ");
    int a6 = input.nextInt();
    appointments.add(new appointment(a1, a2, a3, a4, a5, a6));
    System.out.println(appointments + " Successfully added. \n");
}

【问题讨论】:

  • 认为if (doctors.contains(a3)){ nm 必须是if (!doctors.contains(a3)){,因为如果医生不在列表中,则它是无效的。如果您想在医生有效之前询问,它也必须是一个循环
  • 是的,我尝试在条件之前放置(不是!),并且有 for-loop,仍然一样..当我输入错误或正确的输入时,它会接受它

标签: java class arraylist


【解决方案1】:

一定是这样的:

System.out.println("please enter the doctor name : ");
String a3 = input.nextLine();
while (!doctors.contains(a3)){
        System.out.println("invalid doctor , the doctor doesn't exists. please enter dr.name : ");
        a3 = input.nextLine();
    }
}

迭代直到输入的博士姓名出现在医生列表中。

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2013-01-06
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多