程序运行良好,但嵌套的 do-while 循环不起作用。如果您看到 while 的条件,您可以看到它必须是一个验证,如果它不在这些数字的范围内,它必须再次提出问题。您可以看到,如果主体从程序中退出,则主体必须验证主体不等于 0
我声明:
import java.util.Scanner;
import java.text.DecimalFormat;
public class ACMEMORTGAGE
{
public static void main (String args [])
{
//Declare variables
double rate=0, monthlyPayments, numberMonthlyPayments, paymentAmount;
int mortgageTerm, principal, years=0;
Scanner key=new Scanner(System.in);
DecimalFormat decimalPlaces=new DecimalFormat("$0.00");
do
{
System.out.print("Enter principal amount (0 to end program):");
principal=key.nextInt();
do
{
System.out.print("Enter mortgage amortization (1, 2, 3, 5, 10.):");
mortgageTerm=key.nextInt();
if (mortgageTerm==1)
{
rate=0.035;
}
else if (mortgageTerm==2)
{
rate=0.039;
}
else if (mortgageTerm==3)
{
rate=0.044;
}
else if (mortgageTerm==5)
{
rate=0.05;
}
else if (mortgageTerm==10)
{
rate=0.060;
}
} while (mortgageTerm==1 || mortgageTerm==2 || mortgageTerm==3 || mortgageTerm==5 || mortgageTerm==10);
do
{
System.out.print("Enter mortgage ammortization period (5, 10, 15, 20, 25):");
years=key.nextInt();
} while (years==5 && years==10 && years==15 && years==20 && years==25);
double i, n, x;
i=rate/12;
n=12*years;
monthlyPayments= ((principal*(Math.pow(i+1, n)*(i))) / (Math.pow(i+1, n) - 1));
System.out.print("Monthly payments amount:");
System.out.println(decimalPlaces.format(monthlyPayments));
}while (principal!=0);
}
}