虽然循环不会重新运行程序,但只询问是否要再次循环

时间:2015-10-19 20:02:05

标签: java while-loop

我试图提示用户输入Y,Y或N,n并使用while循环询问他们是否要重新运行程序。我尝试过使用this问题的代码。我改变了String choice = sc.next();到in.next();当我运行程序时一切正常,但是当提示我回答时它只会询问我是否想要在没有实际运行程序的情况下再次运行(让用户输入和输出答案)。 n将正常工作并结束它。感谢您的时间!这让我在这里,youtube和其他编码论坛上寻找答案的许多夜晚。

编辑:我尝试将与贷款相关的代码移动到while循环中(在while和boolean之间),但现在它完全跳过“你想再次计算(y / n):”并直接进入“输入贷款”金额:“不问

package numformexception;

import java.util.Scanner;

public class NumFormException {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    //Declare Variables
    double loanNum;
    double rateNum;
    double trateNum;
    double yearNum;
    double monthNum;
    double equNum;
    double outputNum;

    //Prompt for input
    System.out.print("Enter loan amount: ");
    loanNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter rate: ");
    rateNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter number years: ");
    yearNum = Double.parseDouble(in.nextLine());

    //Postive input data check
    if(loanNum <= 0 || rateNum <=0 || yearNum <=0)
    {
        System.out.println("You must enter positive numeric data! ");
        System.exit(0);
    }

    //Convert years to months 
    monthNum = yearNum * 12;

    //Convert rate to percent and monthly
    trateNum= (rateNum / 100) /12; 

    //Complete numerator 
    equNum = Math.pow((1+ trateNum), monthNum );

    //Plug equNum into formula
    outputNum = trateNum * loanNum * (equNum / (equNum - 1));


    System.out.printf("The monthly payment is: $ %.2f%n", outputNum);


    boolean isContinuing = true;

    while( isContinuing) {
        boolean inputIsInvalid = true;
        while (inputIsInvalid) {
            System.out.print("Would you like to calculate again (y/n): ");

            String choice = in.next();

            if("y".equalsIgnoreCase(choice)) {
                inputIsInvalid = false;

            }
            else if("n".equalsIgnoreCase(choice)){
                inputIsInvalid = false;
                isContinuing = false;
            }
            else {
                System.err.print("Error: Only valid answers are Y/N. ");
            }

        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

您的主要计算不会再次运行,因为它不是此循环的一部分:

while (isContinuing) {
    // ...
}

我建议将主计算逻辑移到一个方法中,例如:

private static void calculateLoan(Scanner in) {
    //Declare Variables
    double loanNum;
    double rateNum;
    double trateNum;
    double yearNum;
    double monthNum;
    double equNum;
    double outputNum;

    //Prompt for input
    System.out.print("Enter loan amount: ");
    loanNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter rate: ");
    rateNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter number years: ");
    yearNum = Double.parseDouble(in.nextLine());

    //Postive input data check
    if (loanNum <= 0 || rateNum <= 0 || yearNum <= 0) {
        System.out.println("You must enter positive numeric data! ");
        System.exit(0);
    }

    //Convert years to months 
    monthNum = yearNum * 12;

    //Convert rate to percent and monthly
    trateNum = (rateNum / 100) / 12;

    //Complete numerator 
    equNum = Math.pow((1 + trateNum), monthNum);

    //Plug equNum into formula
    outputNum = trateNum * loanNum * (equNum / (equNum - 1));

    System.out.printf("The monthly payment is: $ %.2f%n", outputNum);
}

然后更改main方法,以便在循环中调用新的calculateLoan方法:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    boolean isContinuing = true;

    while (isContinuing) {

        calculateLoan(in);

        boolean inputIsInvalid = true;
        while (inputIsInvalid) {
            System.out.print("Would you like to calculate again (y/n): ");

            String choice = in.next();

            if ("y".equalsIgnoreCase(choice)) {
                inputIsInvalid = false;

            } else if ("n".equalsIgnoreCase(choice)) {
                inputIsInvalid = false;
                isContinuing = false;
            } else {
                System.err.print("Error: Only valid answers are Y/N. ");
            }

        }
    }
}

答案 1 :(得分:0)

将循环的唯一代码是在while循环中。因此,完全没有任何与贷款相关的代码会循环。如果你想让它循环,你必须首先把它放在一个循环中,可能在while循环中循环,一个应该包含你的main方法中的几乎所有东西,变量声明之后的所有东西。