如果输入错误,我不知道如何重新启动程序。
我是Java新手,不知道如何有效地使用if语句或循环。
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 Name
System.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();
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 ");}
}
如果积分超过18,我希望程序重新启动if语句。因此,如果输入19,它将说重新输入积分并开始if结束。
答案 0 :(得分:0)
您可以尝试将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);
}
答案 1 :(得分:0)
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("Too many credits");
while(creditNumber > 18);
}
答案 2 :(得分:0)
我会做这样的事情:
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,它就会执行您希望它执行的所有其他操作。请注意,我没有对此进行测试,但是应该可以。
答案 3 :(得分:0)
好吧,我可以建议您重新考虑设计。但这不是你的问题-对吗?因此,如果您确实想要,您想要的就是中断(“可怜的人例外”):
exitpoint:{
//your code ...
break exitpoint;
//more code
break exitpoint;
//....
}
或带有某些循环:
exitpoint:
while( ){
// code....
for(;;){
//...
break exitpoint;
}
}
处理错误(例如错误的用户输入)的一种更好的方法是例外。但这也不是问题-是吗?