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

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

試圖循環(huán)到程序的開頭

試圖循環(huán)到程序的開頭

侃侃爾雅 2022-06-04 09:42:51
如果輸入錯誤,我不知道如何讓程序重新啟動。我是 java 新手,不知道如何有效地使用 if 語句或循環(huán)。import java.util.Scanner; public class TuitionRates {public static void main(String[] args) {    //variables    String name = "";    String studentType = "";    int creditNumber = 0;    double totalTF = 0.00;    double studentACTFee = 4.60;    double parkingFee = 2.00;    double tuition = 180.40;    double capitalFee1 = 0.00;    double capitalFee2 = 21.00;    double perCreditR = 187.00;    double perCreditV = 187.00;    double perCreditD = 187.00;    double perCreditM = 208.00;    double perCreditB = 268.00;    double perCreditO = 387.25;    Scanner input = new Scanner(System.in);    //Asking user for NameSystem.out.println("Welcome to the NOVA Tuition and Fees Calculator.");System.out.println("Please enter your name: ");name= input.nextLine();//Ask user to choose option    System.out.println("Please enter the type of student that your are from     the choices below:\n"            +             "R for Virginia Resident\r\n" +             "M for Military Contract Out-of-State\r\n" +             "V for Military Veterans and Dependents\r\n" +             "D for Dual Enrolled\r\n" +             "B for Business Contract Students\r\n" +             "O for Out of State Students\r\n");    studentType = input.nextLine();if (studentType.equalsIgnoreCase("R"))   {   System.out.println("Please enter the number of credits that you are    taking:");   creditNumber = input.nextInt();如果積分數(shù)超過 18,我希望我的程序重新啟動 if 語句。因此,如果我輸入 19,它會說重新輸入積分并重新開始 if 語句。
查看完整描述

3 回答

?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

您可以嘗試將您的if語句包裝在一個do-while循環(huán)中:


if (studentType.equalsIgnoreCase("R"))

{

    do{

       System.out.println("Please enter the number of credits that you are taking:");

       creditNumber = input.nextInt();


        if (creditNumber <= 18)

        {

            System.out.println("Tuition and fees report for " + name);          

            System.out.println("Tuition: "+ tuition);           

            System.out.println( "Capital Fee:  \t"+ capitalFee1);           

            System.out.println( "Student Activities Fee: \t "+   studentACTFee);            

            System.out.println( "Parking Infrastructure Fee: \t " +  parkingFee);           

            System.out.println("Tuition & Fees Per Credit: " +  perCreditR);            

            System.out.println("X Number of Credits: " + creditNumber);

            totalTF = creditNumber * perCreditR;            

            System.out.println("Total Tuition and Fees: \t" +  totalTF);

            System.out.println("Bodly NOVA");

        }

        else {System.out.println("Please re-enter credit Number ");}

    }while(creditNumber > 18);

}


查看完整回答
反對 回復 2022-06-04
?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

那么我可以建議你重新考慮設計。但這不是你的問題——對吧?所以,如果你真的想要,你所要求的,這里就是一個休息(“窮人例外”):


exitpoint:{


     //your code ...

     break exitpoint;

     //more code

     break exitpoint;

     //....

}

或有一些循環(huán):


exitpoint:

while( ){

    // code....

    for(;;){

        //...

        break exitpoint;

    }

}

處理錯誤(如錯誤的用戶輸入)的更好方法是異常。但這也不是問題——是嗎?


查看完整回答
反對 回復 2022-06-04
?
嚕嚕噠

TA貢獻1784條經(jīng)驗 獲得超7個贊

我會做這樣的事情:


if (studentType.equalsIgnoreCase("R"))

{


System.out.println("Please enter the number of credits that you are 

taking:");

creditNumber = input.nextInt();


while(creditNumber > 18)

{

    System.out.println("Please re-enter the number of credits that you are 

    taking:");

    creditNumber = input.nextInt();

}


System.out.println("Tuition and fees report for " + name);          

System.out.println("Tuition: "+ tuition);           

System.out.println( "Capital Fee:  \t"+ capitalFee1);           

System.out.println( "Student Activities Fee: \t "+   studentACTFee);            

System.out.println( "Parking Infrastructure Fee: \t " +  parkingFee);           

System.out.println("Tuition & Fees Per Credit: " +  perCreditR);            

System.out.println("X Number of Credits: " + creditNumber);

totalTF = creditNumber * perCreditR;            

System.out.println("Total Tuition and Fees: \t" +  totalTF);

System.out.println("Bodly NOVA");

}

這使用了一個 while() 語句來檢查初始 creditNumber 是否大于 18 并不斷地重新提示用戶進行新輸入。然后,一旦它們提供小于或等于 18 的值,它就會執(zhí)行您希望它執(zhí)行的所有其他操作。請注意,我沒有對此進行測試,但它應該可以工作。


查看完整回答
反對 回復 2022-06-04
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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