Java程序循环

时间:2015-10-13 22:39:33

标签: java loops

我希望能够在被问到“再次计算程序”时输入'y'后显示程序(显示输入贷款金额提示)或者如果用户输入'n'则结束程序。

import java.util.Scanner;

public class MonthlyMortgageRate {

    public static void main(String[] args) {

        double Amount;
        double Rate;
        double Months;
        double outputNum1;

        Scanner in = new Scanner(System.in);

        System.out.print("Enter loan amount:");
        Amount = in.nextDouble();

        System.out.print("Enter rate:");
        Rate = in.nextDouble() / 100 / 12;

        System.out.print("Enter year:");
        Months = in.nextDouble() * 12;

        outputNum1 = Rate * Amount / (1 - Math.pow(1 + Rate, -Months));

        if (Amount <= 0)
            System.out.println("You must enter positive numeric data!");
        else
            System.out.printf("Monthly payment is: $ %.2f%n", outputNum1);

        System.out.println("would you like to calculate again?(y/n)");
    }


    private static double inputNum2(double d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

2 个答案:

答案 0 :(得分:2)

基本上你需要在main中循环代码。

do {
    // your code here
    System.out.println("would you like to calculate again?(y/n)");
} while (in.next().equalsIgnoreCase("y"))

答案 1 :(得分:0)

将以下内容添加到您的代码中:

1

享受悬崖