亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何比較字符串

如何比較字符串

千萬里不及你 2023-10-19 21:48:41
我試圖將其帶到用戶按下數字的位置,然后他們輸入他們想要查看的可用選項的字符串。到目前為止,一切正常,除了當用戶輸入字符串時,它沒有給出首選輸出.. *顯示電子郵件而不是顏色、用戶 ID 等。這是我的代碼.. 再次感謝!public static void printStuff(Record[] objects) {   Scanner in = new Scanner(System.in);   System.out.println("Enter the number and record name you would like to see");   int x = in.nextInt();   String bean = in.next();        if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.equals("email")) {            System.out.println(objects[x].getEmail());        }       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.equals("name")) {            System.out.println(objects[x].getfirstName());        }       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("last name")) {            System.out.println(objects[x].getlastName());        }       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("balance")) {            System.out.println(objects[x].getBalance());        }       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("color")) {            System.out.println(objects[x].getColor());        }       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("idnumber")) {            System.out.println(objects[x].getnumID());        } }
查看完整描述

3 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

嘗試一下,始終避免重新輸入案例并使您的代碼更加高效。


public static void printStuff(Record[] objects) {

   Scanner in = new Scanner(System.in);

   System.out.println("Enter the number and record name you would like to see");

   int x = in.nextInt();

   String bean = in.next();



        if (x >= 1 && x =< 5 ) {

            if (bean.equals("email"))

               System.out.println(objects[x].getEmail());

            else if (bean.equals("name"))

               System.out.println(objects[x].getfirstName());

            else if (bean.matches("last name"))

               System.out.println(objects[x].getlastName());

            else if (bean.matches("balance"))

               System.out.println(objects[x].getBalance());

            else if (bean.matches("color"))

               System.out.println(objects[x].getColor());

            else if (bean.matches("idnumber"))

               System.out.println(objects[x].getnumID());

        }

 }


查看完整回答
反對 回復 2023-10-19
?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

在所有 if 條件中,&&的優先級高于||,因此必須將條件更改為:

(x == 1 || x == 2 || x == 3 || x == 4 || x == 5) && bean.equals("email").

這對應于您想要的邏輯,如果x1 到 5 中的某個值 AND bean 等于"email"。但是,請研究比較運算符,因為您可以將其簡化為:

(1 <= x && x <= 5) && bean.equals("email").


查看完整回答
反對 回復 2023-10-19
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

也許更容易閱讀:


if (1 <= x && x <= 5) {

? ? switch (bean) {

? ? ? ? case "email":?

? ? ? ? ? ? System.out.println(record.email);

? ? ? ? ? ? break;

? ? ? ? case "name":?

? ? ? ? ? ? System.out.println(record.firstName);

? ? ? ? ? ? break;

? ? ? ? ...

? ? }

}

使用switch 表達式(Java 13,?--enable-preview):


if (1 <= x && x <= 5) {

? ? System.out.println(switch (bean) {

? ? ? ? case "email" -> record.email;

? ? ? ? case "name" -> record.firstName;

? ? ? ? case "last name"-> record.lastName;

? ? ? ? ...

? ? ? ? default -> "unrecognized " + bean;

? ? ? ? // default -> throw new IllegalArgumentException(...);

? ? });

}

或者,如果對未知不執行任何操作bean:


if (1 <= x && x <= 5) {

? ? switch (bean) {

? ? ? ? case "email" -> System.out.println(record.email);

? ? ? ? case "name" -> System.out.println(record.firstName);

? ? ? ? ...

? ? }

}


查看完整回答
反對 回復 2023-10-19
  • 3 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號