3 回答

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);
}

TA貢獻1813條經(jīng)驗 獲得超2個贊
那么我可以建議你重新考慮設計。但這不是你的問題——對吧?所以,如果你真的想要,你所要求的,這里就是一個休息(“窮人例外”):
exitpoint:{
//your code ...
break exitpoint;
//more code
break exitpoint;
//....
}
或有一些循環(huán):
exitpoint:
while( ){
// code....
for(;;){
//...
break exitpoint;
}
}
處理錯誤(如錯誤的用戶輸入)的更好方法是異常。但這也不是問題——是嗎?

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í)行的所有其他操作。請注意,我沒有對此進行測試,但它應該可以工作。
添加回答
舉報