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

时间:2016-04-19 05:35:32

标签: java string compare

我现在知道了,并且现在正在掌握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语句我输入option.equalsIgnoreCase(“end”),我是否解释了计算机执行以下操作?

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

2.当String选项不是单词end时,让计算机在if语句中执行操作吗?

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

2 个答案:

答案 0 :(得分:0)

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));                
}

第一种方法equals()比较str1str2并考虑案例。因此,第一次比较结果为false,这意味着啤酒不等于啤酒。

第二种方法equalsIgnoreCase()执行相同的操作,之外 区分大小写。这种比较的结果是true,意思是"忽略这种情况,啤酒与啤酒是相同的字符串"。

希望这有帮助。

答案 1 :(得分:0)

option.equalsIgnoreCase(" end") - equalsIgnoreCase将忽略string是小写还是大写。

因此,只有当期权变量有结束或结束时才会进入if。

你的第一个假设是正确的,你要求比较String是否等于end。但是第二个是错的,如果只有当选项是结束/结束时,从上面的代码它将进入并执行内部存在的语句。

如果你想进入内部如果选项未结束时阻止,那么添加一个不是这样的if(!option.equalsIgnoreCase(" end"))。

我希望这能清除你的怀疑!