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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

需要一個(gè)不滿(mǎn)足的 if 語(yǔ)句來(lái)轉(zhuǎn)到代碼開(kāi)頭

需要一個(gè)不滿(mǎn)足的 if 語(yǔ)句來(lái)轉(zhuǎn)到代碼開(kāi)頭

慕斯709654 2023-07-28 16:20:01
我正在嘗試設(shè)置一些 if 語(yǔ)句,以確保用戶(hù)輸入不會(huì)超出我設(shè)置的范圍(如果我希望 if 語(yǔ)句將它們返回到主代碼的開(kāi)頭)。String animal = JOptionPane.showInputDialog("Enter In Animal Name"); // Asking for user to enter a animalString fruit = JOptionPane.showInputDialog("Enter In A Fruit Name"); // Asking user to enter a fruitint days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));// Above integer is asking the user to enter the amount of days.// Below if statements are basically error checking to ensure the user stays between the// range asked for when they are asked to enter in days between 1 and 10.if (days <= 0) { // Ensures that negative numbers cannot be entered.    JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");    return;}if (days >= 10) { // Ensures nothing over 10 can be entered.    JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");    return;}如果 if 語(yǔ)句說(shuō)它是一個(gè)錯(cuò)誤,它應(yīng)該返回要求他們重新輸入天數(shù)
查看完整描述

4 回答

?
冉冉說(shuō)

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

使用do-while至少執(zhí)行一次的循環(huán),如果days不滿(mǎn)足條件則每次都循環(huán)回來(lái)。


public static void main(String[] args) 

{

    do {

        //Your other code

        String animal = JOptionPane.showInputDialog("Enter In Animal Name"); // Asking for user to enter a animal

        String fruit = JOptionPane.showInputDialog("Enter In A Fruit Name"); // Asking user to enter a fruit


        int days = askForInput();


        if (days <= 0 || days >= 10) { // Ensures that negative numbers cannot be entered.

            JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");

        }


    } while (days <= 0 || days >= 10);

}


//Pass whatever parameters you might need

public static int askForInput() {

    int days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));

    //Any other code you want

    return days;

}

我還將它提取到一個(gè)方法中,這可能是不必要的,但如果需要,它可以讓您添加更多功能。


如果您不想每次都被問(wèn)到這個(gè)問(wèn)題,您也可以移動(dòng)animal或fruit離開(kāi)。do


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

允許代碼重新啟動(dòng)代碼塊(即返回到開(kāi)頭并重試)的一種方法是使用帶有“永遠(yuǎn)”循環(huán)的continueand語(yǔ)句。break


for (;;) { // loop forever

    // some code here

    if (failure condition 1) {

        // handle failure here

        continue; // go back and try again

    }

    if (failure condition 2) {

        // handle failure here

        continue; // go back and try again

    }

    // more code and failure condition checks here

    break; // unconditional exit loop, since all is ok

}

如果“此處的某些代碼”本身位于循環(huán)內(nèi),但需要返回到開(kāi)頭并重試,則可以為此使用標(biāo)簽:


TRYAGAIN: for (;;) { // loop forever

    // some code here

    for (some looping here) {

        // some code here

        try {

            if (failure condition) {

                // handle failure here

                continue TRYAGAIN; // go back and try again

            }

        } finally {

            // code here will execute, even if 'continue' is used

        }

    }

    // more code and failure condition checks here

    break; // unconditional exit loop, since all is ok

}


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

String animal;

String fruit;

int days = 0;


animal = JOptionPane.showInputDialog("Enter In Animal Name");

fruit = JOptionPane.showInputDialog("Enter In A Fruit Name");


while(days <= 0 || days > 10) {

    days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));


    if (days <= 0 || days > 10) {

        JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
臨摹微笑

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

使用 while 循環(huán)


// Your user prompt for animal and fruit goes here


boolean exit = false;

while(!exit)

{

    // Your user prompt for days code goes here


    if (days <= 0 || days > 10) {

        JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");

        exit = false; // This is not necessary but nice for readability's sake

    }

    else {

        exit = true;

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-28
  • 4 回答
  • 0 關(guān)注
  • 194 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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