java - 试图循环到程序的开头
问题描述
如果输入错误,我不知道如何让程序重新启动。
我是 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 语句。
解决方案
您可以尝试将您的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);
}
推荐阅读
- android - Android:总是使用 postInvalidate() 而不是 invalidate() 有缺点吗?
- laravel - 模块构建失败:TypeError:this.setDynamic 不是函数
- java - 使用带有接口的抽象类的设计问题
- javascript - 如何遍历对象并将其值附加到网页
- visual-studio-code - 如何在 VS Code 的资源管理器侧边栏中删除文件名之外的文件路径文本?
- javascript - 在 addEventListener 之外声明的 JavaScript 函数不起作用
- r - ggplot标题背景颜色/填充
- excel - Excel Worksheet_Change 禁用自身
- r - 查找与排序向量比较的元素索引
- highcharts - 使用类别代替极坐标导出 Highcharts 极坐标图 csv