简体   繁体   English

带嵌套循环的Java验证

[英]Java validation with nested loops

The program runs good but the nested do-while loop is not working. 该程序运行良好,但是嵌套的do-while循环无法正常工作。 If you see the condition of the while you can see that it have to be a validation and if it is not in the range of those numbers it has to ask the questions again. 如果您看到了一段时间的条件,则可以看到它必须是一个验证,并且如果它不在这些数字的范围内,则必须再次提出问题。 You can see that the principal do- while has to validate that principal is not equal to 0 if it is it goes out from the program 您可以看到,如果委托人程序从程序中退出,则它必须验证委托人不等于0。

I declare: 我声明:

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);

   }

}

You are using the assignment operator ( = ) instead of the comparison operator ( == ) in each if and this is a mistake. 您正在使用赋值运算符( = ),而不是比较操作符( ==每个) if这是一个错误。 While in C you might do it (and it usually an indication of a bug), in Java you have to use boolean expressions inside of an if clause. 在C语言中,您可以执行此操作(通常表示错误),而在Java中,必须在if子句中使用布尔表达式。 Should be: 应该:

if (years == 1)

And so on. 等等。

if (years=1)

Here you are assigning a value 1 to your years . 在这里,您将为years分配值1 So the result of the expression in your if is not a boolean expression. 因此, if expression的结果不是boolean表达式。

Now, since Java expects the condition inside if to evaluate to a boolean value, so it gives incomaptible types. 现在,由于Java期望if的条件求值为boolean值,因此它给出了不兼容的类型。

You should use: - 您应该使用:-

if(years == 1)

Similarly, you need to change your while statement to: - 同样,您需要将while语句更改为:-

while (years==1 && years==2 && ...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM