我不完全知道 .equalsIgnoreCase 是如何工作的?



我现在确实知道并掌握了java的窍门,我正在编写一个帮助记录医院患者ID的程序,我会先显示整个代码,然后,我会告诉你在哪里,这是代码

package hospitalsrecord;
import java.util.*;
import java.io.*;
public class HospitalsRecord {
public static Scanner read = new Scanner(System.in);
public static ArrayList nameList = new ArrayList();
public static ArrayList patientAge = new ArrayList();
public static ArrayList Disease = new ArrayList();
public static ArrayList dateHospitalized = new ArrayList();
public static ArrayList roomNumber = new ArrayList();
//adding patient function
    public static void AddNewPatient () {  
    //Ask patient's name
    System.out.println("Please enter patient's name:");
    String patientName = read.next();
    //Ask Patient's age
    System.out.println("Please enter patient's age:");
    int age = read.nextInt();   
    //Ask patient's illness
    System.out.println("Please enter patient's Disease name (also include accidents eg. Leg broke by Car Accident):");
    String illness = read.next();    
    //Ask patient Hospitalized date
    System.out.println("Please enter patient's Hospitalized date(Total days not included):");
    String HPTLdate = read.next();    
    //Ask patient's room number
    System.out.println("Please enter patient's hospitalize room number(3 degits):");
    int HRN = read.nextInt();
    //Confirmation
    System.out.println("Doctor, would you like to confirm the following(y/n)?");
    System.out.println("Name:" + patientName);
    System.out.println("Age:" + age);
    System.out.println("Disease:" + illness);
    System.out.println("Date Hospitalized (HPTLD):" + HPTLdate);
    System.out.println("Room Number:" + HRN);
    String Confirm = read.next();
    if (Confirm.equals("y")) {
      nameList.add(patientName);
    patientAge.add(age);
    Disease.add(illness);
    dateHospitalized.add(HPTLdate);
    roomNumber.add(HRN);
    } else {
        AddNewPatient();
    }
    }
    //Searching patient that listed
    public static void searchPatient (){
    }
    //remove the patient function
    public static void removePatient() {
    }
    //text printing function when strat the program
    public static void selectorPage(){
        System.out.println("Hello Doctor, welcome to Hospital Recorder v1.0.0");
        System.out.println("If you want to add new patient into this recorder type: 'add' in the next blank line line");
        System.out.println("If you want to search the patient list type: 'search' in the next blank line");
        System.out.println("And, if you want to remove the patient that was out of hospitalizing type: 'remove' in the next blank line");
        option = read.next();
    }
    //text printing simmilar to selecterPage function but perform after function
    public static void selecterPageAfterAction() {
        System.out.println("Your action has been performed, doctor");
        System.out.println("Would you like to perform another action?(y/n)");
       choiceSelection = read.next();
       if (choiceSelection.equals("y")){
        System.out.println("If you want to add new patient into this recorder type: 'add' in the next blank line line");
        System.out.println("If you want to search the patient list type: 'search' in the next blank line");
        System.out.println("And, if you want to remove the patient that was out of hospitalizing type: 'remove' in the next blank line");
        option = read.next();
       }  
    }
    //Selection var
    public static String option;
    public static String choiceSelection;
    //Main program
    public static void main(String[] args) {
        selectorPage();
        switch (option) {
            case("add"): {
                AddNewPatient();
                break;
            }
            case("search"):{
                searchPatient();
                break;
            }
            case("remove"):{
                removePatient();
                break;
            }
            case("end"):{
                break;
            }
            default: {
                System.out.println("Please enter the indentified option");
                break;
            }
        }
        if (option.equalsIgnoreCase("end")){
        }
    }
}

我希望你们能阅读每一行,因为它太复杂了,但对于一个能阅读所有内容的人来说,我知道你们会说我仍然需要更多的时间来努力工作,不用担心,我会花一些时间首先从你们那里获得大部分知识,但在等待答案的同时,我仍然努力完成程序!不管怎样,我希望你们在这一点上关注的是:

if (option.equalsIgnoreCase("end")){
    }

它可能太空了,因为我刚在处理它的时候添加了它。所以,我想知道的是在if语句中我键入的选项。equalsIgnoreCase("end"),我能解释计算机执行以下操作吗?

1.将字符串变量选项与字符串"end"进行比较?

2.当字符串选项不是单词结尾时,告诉计算机在if语句中执行操作?

请告诉我这个方法是如何工作的,我不太清楚。我这样理解"它比较两个字符串,如果不相同,那么结果是真的"。我知道我的解释是错误的,所以你能帮我吗?如果可以的话,再次感谢你的帮助。

option.equalsIgnoreCase("end")-equalsIgnoleCase将忽略字符串是小写还是大写。

因此,只有当选项变量具有end或end时,它才会进入if块。

您的第一个假设是正确的,您要求比较String是否等于end。但第二个是错误的,从上面的代码中,它将只在选项为end/end时输入并执行内部存在的语句。

如果你想在选项没有结束时进入If块,那么添加一个不像这样的If(!option.equalsIgnoreCase("end")).

我希望这能消除你的疑虑!

String有两个方法来比较一个String与另一个String。

参见以下示例:

public static void main(String[] args) {
    String str1 = "beer";
    String str2 = "Beer";
    System.out.println(str1.equals(str2));
    System.out.println(str1.equalsIgnoreCase(str2));                
}

第一种方法CCD_ 2比较CCD_ 3和CCD_。因此,第一个比较结果为false,这意味着啤酒不等于啤酒。

第二种方法equalsIgnoreCase()也做了同样的事情,除了不区分大小写。这个比较的结果是true,意思是"忽略大小写,啤酒和啤酒是同一个字符串"。

希望这能有所帮助。

相关内容

  • 没有找到相关文章

最新更新