试图循环到程序的开头

如果输入错误,我不知道如何让程序重新启动。


我是 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();



如果积分数超过 18,我希望我的程序重新启动 if 语句。因此,如果我输入 19,它会说重新输入积分并重新开始 if 语句。


侃侃尔雅
浏览 89回答 3
3回答

ibeautiful

您可以尝试将您的if语句包装在一个do-while循环中:if (studentType.equalsIgnoreCase("R")){&nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Please enter the number of credits that you are taking:");&nbsp; &nbsp; &nbsp; &nbsp;creditNumber = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (creditNumber <= 18)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Tuition and fees report for " + name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Tuition: "+ tuition);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "Capital Fee:&nbsp; \t"+ capitalFee1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "Student Activities Fee: \t "+&nbsp; &nbsp;studentACTFee);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "Parking Infrastructure Fee: \t " +&nbsp; parkingFee);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Tuition & Fees Per Credit: " +&nbsp; perCreditR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("X Number of Credits: " + creditNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalTF = creditNumber * perCreditR;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total Tuition and Fees: \t" +&nbsp; totalTF);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Bodly NOVA");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {System.out.println("Please re-enter credit Number ");}&nbsp; &nbsp; }while(creditNumber > 18);}

慕姐8265434

那么我可以建议你重新考虑设计。但这不是你的问题——对吧?所以,如果你真的想要,你所要求的,这里就是一个休息(“穷人例外”):exitpoint:{&nbsp; &nbsp; &nbsp;//your code ...&nbsp; &nbsp; &nbsp;break exitpoint;&nbsp; &nbsp; &nbsp;//more code&nbsp; &nbsp; &nbsp;break exitpoint;&nbsp; &nbsp; &nbsp;//....}或有一些循环:exitpoint:while( ){&nbsp; &nbsp; // code....&nbsp; &nbsp; for(;;){&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; break exitpoint;&nbsp; &nbsp; }}处理错误(如错误的用户输入)的更好方法是异常。但这也不是问题——是吗?

噜噜哒

我会做这样的事情:if (studentType.equalsIgnoreCase("R")){System.out.println("Please enter the number of credits that you are&nbsp;taking:");creditNumber = input.nextInt();while(creditNumber > 18){&nbsp; &nbsp; System.out.println("Please re-enter the number of credits that you are&nbsp;&nbsp; &nbsp; taking:");&nbsp; &nbsp; creditNumber = input.nextInt();}System.out.println("Tuition and fees report for " + name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;System.out.println("Tuition: "+ tuition);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println( "Capital Fee:&nbsp; \t"+ capitalFee1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println( "Student Activities Fee: \t "+&nbsp; &nbsp;studentACTFee);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;System.out.println( "Parking Infrastructure Fee: \t " +&nbsp; parkingFee);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Tuition & Fees Per Credit: " +&nbsp; perCreditR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;System.out.println("X Number of Credits: " + creditNumber);totalTF = creditNumber * perCreditR;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;System.out.println("Total Tuition and Fees: \t" +&nbsp; totalTF);System.out.println("Bodly NOVA");}这使用了一个 while() 语句来检查初始 creditNumber 是否大于 18 并不断地重新提示用户进行新输入。然后,一旦它们提供小于或等于 18 的值,它就会执行您希望它执行的所有其他操作。请注意,我没有对此进行测试,但它应该可以工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java