第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何比較字符串

如何比較字符串

千萬里不及你 2023-10-19 21:48:41
我試圖將其帶到用戶按下數(shù)字的位置,然后他們輸入他們想要查看的可用選項(xiàng)的字符串。到目前為止,一切正常,除了當(dāng)用戶輸入字符串時,它沒有給出首選輸出.. *顯示電子郵件而不是顏色、用戶 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貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超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());

        }

 }


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

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個贊

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

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

這對應(yīng)于您想要的邏輯,如果x1 到 5 中的某個值 AND bean 等于"email"。但是,請研究比較運(yùn)算符,因?yàn)槟梢詫⑵浜喕癁椋?/p>

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


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

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超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 表達(dá)式(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(...);

? ? });

}

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


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

? ? switch (bean) {

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

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

? ? ? ? ...

? ? }

}


查看完整回答
反對 回復(fù) 2023-10-19
  • 3 回答
  • 0 關(guān)注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號