Java Loops validation not woorking - help

Sharky Forums


Results 1 to 2 of 2

Thread: Java Loops validation not woorking - help

  1. #1
    Expensive Sushi
    Join Date
    Oct 2012
    Posts
    9

    Java Loops validation not woorking - help

    I'm trying to do a nested if inside a loop. The user has to enter a period between 5, 10, 15, 20, 25 years. Each year has a different rate and if the user enter a number that is not within these numbers the question is ask again.

    The program runs good but the nested do-while loop is not working. If you see the condition of the while then 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. And if you type 0 it ends the progam. The validation it's not working and I don't not why

    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class ACMEMORTGAGE
    {
    public static void main (String args [])

    {
    //Declare variables
    double principal, rate=0;
    int mortgageTerm;

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




    }while (principal!=0);

    }

    }

  2. #2
    Tiger Shark linoleum's Avatar
    Join Date
    Feb 2003
    Posts
    899
    Quote Originally Posted by shinju View Post
    while (mortgageTerm==1 && mortgageTerm==2 && mortgageTerm==3 && mortgageTerm==5 && mortgageTerm==10);
    mortgageTerm is never going to equal all five of those values at once.

    I think you want (mortageTerm!=1 && ..etc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •