3 回答
TA贡献1993条经验 获得超5个赞
您可以尝试将您的if语句包装在一个do-while循环中:
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条经验 获得超2个赞
那么我可以建议你重新考虑设计。但这不是你的问题——对吧?所以,如果你真的想要,你所要求的,这里就是一个休息(“穷人例外”):
exitpoint:{
//your code ...
break exitpoint;
//more code
break exitpoint;
//....
}
或有一些循环:
exitpoint:
while( ){
// code....
for(;;){
//...
break exitpoint;
}
}
处理错误(如错误的用户输入)的更好方法是异常。但这也不是问题——是吗?
TA贡献1784条经验 获得超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 的值,它就会执行您希望它执行的所有其他操作。请注意,我没有对此进行测试,但它应该可以工作。
添加回答
举报