简体   繁体   English

在 for 循环中打印不需要的 else 语句

[英]Printing an unwanted else statement in a for loop

So I'm reading from a text file with the following format所以我正在读取具有以下格式的文本文件

Type |类型 | ID |身份证 | Name |姓名 | Date |日期 | Doctor |博士 | Symptom症状

I have a method to reschedule an appointment where a promt is shown to enter an ID number, then it looks at each line in the text file to look for the ID entered.我有一种重新安排约会的方法,其中显示提示以输入 ID 号,然后它查看文本文件中的每一行以查找输入的 ID。 If the ID matches with the one in the text file... It shows the appointment and asks to enter a new date.如果 ID 与文本文件中的 ID 匹配...它会显示约会并要求输入新日期。 If it doesn't match it says "no coming appointment for the entered ID"如果不匹配,则显示“输入的 ID 没有预约”

The problem I'm having is that when it find the ID it prints that it doesn't match then later finds it.我遇到的问题是,当它找到它打印的 ID 时它不匹配,然后再找到它。

It's clearly an issue with the nested for if loop.这显然是嵌套 for if 循环的问题。

Text File:文本文件:

Emergency|32456|Mohammed Al Azri|12-11-2021 09:30|Dr. Muna Mousa|fever, cough
Routine|12345|Ali Al Abri|22-11-2021 10:30|Dr. Ahmed Al Abri|blood, x-ray
Routine|32456|Mohammed Al Azri|02-12-2021 08:45|Dr. Hisham Nazim|x-ray
Emergency|12345|Ali Al Abri|12-11-2021 08:15|Dr. Ahmed Al Abri|fever, cold, cough
Routine|43234|Mariam Ali|24-11-2021 09:15|Dr. Muna Mousa|blood, urine
Emergency|44342|Issa Ismail|13-11-2021 13:15|Dr. Muna Mousa|fever

Code Snippet:代码片段:

static void rescheduleAppointment()
    {
        System.out.print("Enter Patient ID: ");
        int id = input.nextInt();
        input.nextLine();

        for (int i = 0; i < schedule.size(); i++)
        {
            if (id == schedule.get(i).getPatientID())
            {
                System.out.println("The coming scheduled appointment for " + schedule.get(i).getPatientName() + "(ID#: " + schedule.get(i).getPatientID() + ") on " + schedule.get(i).getAppointmentTime());

                System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
                String dateAndTime = input.nextLine();
                schedule.get(i).setAppointmentTime(dateAndTime);

                System.out.println("Appointment has been updated");
            }
            else
            {
                System.out.println("No coming appointment found for " + id +".");
                System.out.println("You might need to schedule a new appointment.\n");
            }
        }
    }

Output if input is 12345: Output 如果输入为 12345:

Enter Patient ID: 12345
No coming appointment found for 12345.
You might need to schedule a new appointment.

The coming scheduled appointment for Ali Al Abri(ID#: 12345) on 22-11-2021 10:30
Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm :

I'm guessing it sees the first line and ID doesn't match with the input, so it goes for the else statement, but in the next line when it matches it then goes for the if statement.我猜它看到第一行并且 ID 与输入不匹配,所以它适用于 else 语句,但在下一行匹配时,它适用于 if 语句。 What's the solution?解决方案是什么? Sorry in advance if the solution is very simple, I can't see it I've been coding all night:)提前抱歉,如果解决方案很简单,我看不到它我已经编码了一夜:)

One way to solve this is to create a boolean variable patientIdFound that would represent whether or not the patient ID was found in the text file.解决此问题的一种方法是创建一个 boolean 变量patientIdFound ,该变量表示是否在文本文件中找到了患者 ID。

static void rescheduleAppointment()
    {
        System.out.print("Enter Patient ID: ");
        int id = input.nextInt();
        input.nextLine();

        boolean patientIdFound = false;

        for (int i = 0; i < schedule.size(); i++)
        {
            if (id == schedule.get(i).getPatientID())
            {
                patientIdFound = true;
                System.out.println("The coming scheduled appointment for " + schedule.get(i).getPatientName() + "(ID#: " + schedule.get(i).getPatientID() + ") on " + schedule.get(i).getAppointmentTime());

                System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
                String dateAndTime = input.nextLine();
                schedule.get(i).setAppointmentTime(dateAndTime);

                System.out.println("Appointment has been updated");
                break;
            }
        }

        if (!patientIdFound) {
            System.out.println("No coming appointment found for " + id +".");
            System.out.println("You might need to schedule a new appointment.\n");
        }
    }

I included break statement to stop the for loop once the patient ID is found.一旦找到患者 ID,我包含了break语句来停止for循环。

    boolean found = false;
    for (int i = 0; i < schedule.size(); i++)
    {
        if (id == schedule.get(i).getPatientID())
        {
            System.out.printf("The coming scheduled appointment for %s(ID#: %s) on %s%n",
                schedule.get(i).getPatientName(),
                schedule.get(i).getPatientID(),
                schedule.get(i).getAppointmentTime());

            System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
            String dateAndTime = input.nextLine();
            schedule.get(i).setAppointmentTime(dateAndTime);

            System.out.println("Appointment has been updated");

            found = true;
            break;
        }
    }
    if (!found)
    {
        System.out.println("No coming appointment found for " + id +".");
        System.out.println("You might need to schedule a new appointment.");
        System.out.println():
    }

Only after every element was inspected, after the loop you can conclude failure.只有在检查了每个元素之后,在循环之后,您才能得出失败的结论。 Success can break out of the loop.成功可以break循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM