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

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

Java,創(chuàng)建一個條件語句,如果沒有元素的名稱與輸入匹配,則發(fā)送消息?

Java,創(chuàng)建一個條件語句,如果沒有元素的名稱與輸入匹配,則發(fā)送消息?

qq_遁去的一_1 2023-09-06 17:07:01
如果缺少信息,我提前道歉,這對我來說是新的!所以作業(yè)說;有一家寵物酒店(狗、貓和蛇),該程序應(yīng)該打印出他們應(yīng)該吃多少食物以及什么類型的食物。用戶寫下寵物的名字,它應(yīng)該打印出來。我不明白的是如何編寫一個條件語句,該條件語句表示如果沒有元素的名稱與輸入匹配,則寫“我們酒店沒有具有該名稱(輸入)的寵物”。我遇到這個問題的原因是因為我無法到達(dá)任何元素,除非我創(chuàng)建一個 foreach 循環(huán),并且我不希望在循環(huán)結(jié)束后為每個元素彈出一條消息。import javax.swing.*;public class Main {public static void main(String[] args) {    Dog d1 = null;    Dog d2 = null;    Cat c1 = null;    Cat c2 = null;    Snake s1 = null;    try {        d1 = new Dog("Sixten", 5);        d2 = new Dog("Dogge", 10);        c1 = new Cat("Venus", 5);        c2 = new Cat("Ove", 3);        s1 = new Snake("Hypno", 1);    } catch (IllegalArgumentException e) {        System.out.println(e.getMessage());        System.exit(0);    }    Hotel h1 = new Hotel();    h1.addPet(d1);    h1.addPet(d2);    h1.addPet(c1);    h1.addPet(c2);    h1.addPet(s1);    h1.getPets(); // gets the list with all the pets    while (true) {        String input = JOptionPane.showInputDialog("What pet(name of pet) needs feeding?");        if (input == null){            System.exit(0);            break;        }        else if(input.equals("")){            JOptionPane.showMessageDialog(null, "Invalid input!");        }// HERE IS WHERE I WANT THE STATEMENT        else if(**Statement that says if input isn't equal to any of the animal's name**){        }        else{            input = input.toLowerCase();            for(Pet pet: h1.getPets()){                String text1 = String.format("%s%10s%10s\n", "Namn:", "M?tt:", "Sort:");                String text2 = String.format("%s%10.2f%16s", pet.getName(), pet.measureFood(), pet.getFoodName());                String text3 = "---------------------------------------\n";                text1 = text1 + text3 + text2;                if (pet.getName().toLowerCase().equals(input)) {                    JOptionPane.showMessageDialog(null,text1);                    break;                }            }        }    }}}
查看完整描述

2 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗 獲得超2個贊

事實上,你不需要額外的條件。


如果您編寫一個單獨的條件來檢查是否沒有寵物的名字與輸入匹配,那么您將迭代寵物列表兩次,這是多余的。


請注意,如果發(fā)現(xiàn)寵物,if內(nèi)部for將被運(yùn)行。我們可以boolean在 中將變量設(shè)置為 true if,并在循環(huán)后檢查它是否找到寵物:


// in the else branch of the outermost if

boolean petFound = false; // note this line

input = input.toLowerCase();


for(Pet pet: h1.getPets()){


    String text1 = String.format("%s%10s%10s\n", "Namn:", "M?tt:", "Sort:");

    String text2 = String.format("%s%10.2f%16s", pet.getName(), pet.measureFood(), pet.getFoodName());

    String text3 = "---------------------------------------\n";

    text1 = text1 + text3 + text2;



    if (pet.getName().toLowerCase().equals(input)) {

        JOptionPane.showMessageDialog(null,text1);

        petFound = true; // note this line

        break;

    }

}

if (!petFound) {

    // show the message that there is no pet with the input name

}


查看完整回答
反對 回復(fù) 2023-09-06
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗 獲得超5個贊

您可以使用一個標(biāo)志“petFound”,如果找到了 pet,則在 for 循環(huán)中將其設(shè)置為 true。在循環(huán)后檢查標(biāo)志值,如果標(biāo)志為假,則打印未找到消息。


如果您正在研究 Java8,請?zhí)鎿Q循環(huán)


Optional<Pet> pet = h1.getPets().stream().filter(pet.getName().toLowerCase().equals(input)).findFirst();

if(pet.isPresent()){

    pet.get();// gives the pet

}

else{

    // Print pet not found

}


查看完整回答
反對 回復(fù) 2023-09-06
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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