简体   繁体   中英

Why won't my Java while loop stop and let me program terminate?

import java.util.*;

public class Investment 
{

    public static int years=0;
    public static void main(String[] args) 
    {
        Scanner kbd = new Scanner(System.in);
        System.out.println("Please enter an integer representing the whole dollar value of your initial investment: $");
        int startBal=kbd.nextInt();
        System.out.println("Please enter the whole number percentage, excluding the percent sign, of the interest rate on the investment.");
        int interestRate=kbd.nextInt()/100;
        int endBal=startBal*2;
        int currentBal=startBal;
        while (endBal>=currentBal)
        {
            currentBal=currentBal*interestRate+currentBal;
            years++;
        }
        System.out.println("It will take "+years+" years to double your investment.");
    }
}

The output I'm seeing is:

Please enter an integer representing the whole dollar value of your initial investment: $
10000
Please enter the whole number percentage, excluding the percent sign, of the interest rate on the investment.
5

The "10000" and "5" are my input. The program should be printing my final statement with the answer "15," but instead doesn't do anything, and doesn't terminate.

int interestRate=kbd.nextInt()/100;

Since interestRate is an int , it gets rounded down to zero.

So this line:

currentBal=currentBal*interestRate+currentBal;

resolves to:

=> currentBal=currentBal*0+currentBal;
=> currentBal=0+currentBal;
=> currentBal=currentBal;

So the value never increases, so will never reach double its initial value, so infinite loop.

You would have to replace the line with:

double interestRate=kbd.nextInt()/100.0;

Your interestRate is declared as an int so it will be set to 0.

You should declare all values as doubles, since we-re talking about money we want to be precise :)

You have an int interest rate, which when divided by 100 , becomes 0 . Make it a double and use the double literal 100.0 to divide, to force floating-point division.

double interestRate=kbd.nextInt()/100.0;

The currentVal variable will need to be a double also.

My output with the changes:

Please enter an integer representing the whole dollar value of your initial investment: $
10000
Please enter the whole number percentage, excluding the percent sign, of the interest rate on the investment.
5
It will take 15 years to double your investment.

This will let the program finish. But normally it's not a good idea to use a double to store money .

Your doing integer division 5/100 which = 0 remainder 5. So in your calculation you are multiplying by 0 so your current balance will always be 0 and never reach end balance. Try using a double for your interest rate rather than an integer.

while (endBal>=currentBal)
{
    currentBal=currentBal*interestRate+currentBal;
    years++;
}

of course it never terminate, you are only incrementing years but the variables on the while are endBal and currentBal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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