While Loop Problem with Java Assignment Program
Hey guys as part of assignment I have been asked to build a Java Programme that will calculate the number of Years it takes to pay off a Student Loan, I have got the Calculations within the Program right but seem to be having problems with the While Loop it will run, but only go round 1 or maybe 2 times could you look at my code for me and let me know if you spot any problems that I may be missing that is stopping the Loop Implementing Properly.
Code:
public class LoanPayments {
// place constant declarations (static final) below.
// e.g. 3% interest, 9% repayment, 5% pay rise and 10k theshold
static final double INTEREST = 0.03, REPAID = 0.09, PAYRISE = 0.05, NET = 10000;
public static void main(String args[]) {
double loans, prevloan, newloan, wages, net, interest, newlinterest=0, linterest=0, wagest=0, newwages=0, repaid=0, balance=0, newbalance=0, yrbal=0, year=1;
// place all variable declarations here
System.out.println(" Student Loan" );
loans = UserInput.readDouble();
// prompt and read the loan amount
System.out.println (" Wages ");
wages = UserInput.readDouble();
interest = loans * INTEREST;
// add 3% to the loan
net = wages - NET;
// deduct 10k from the wages
repaid = net * REPAID;
// from the remaining wages calculate 9% repayment
linterest = loans + interest;
balance = linterest - repaid;
// reapaid loan amount
wagest = net;
yrbal= balance * year;
// sum up repayments and years so far
System.out.println(" Year " + (int)year);
MoneyOut.println(" Old Loan ", (int)loans);
// produce output: current year, old loan,
MoneyOut.println(" Loans plus 3% interest ", linterest);
// loan plus 3% interest, wages,
MoneyOut.println(" Wages ", (int)wages);
MoneyOut.println(" Wages minus threshold ", wagest);
//wages minus 10,000 pounds, repayment, new loan
MoneyOut.println(" Repayment ", (int)repaid);
MoneyOut.println(" New loan amount ", balance);
while (newbalance>0) {
// while loop starts here
year++;
prevloan = balance;
newloan = prevloan;
interest = newloan * INTEREST;
// add 3% to the loan
newwages = wages + (wages * 5.0 /100.0);
// calculate new wages by adding a pay rise of 5%
net = newwages - NET;
// deduct 10k from the wages
repaid = net * REPAID;
// from the remaining wages calculate 9% repayment
//if (newwages <0) {
// repaid =0;
//}
// (if the remaining wages are zero or less, repayment is zero)
newlinterest = newloan + interest;
newbalance = newlinterest - repaid;
// reapaid loan amount
wagest = net;
yrbal= newbalance * year;
// sum up repayments and years so far
System.out.println(" Year " + (int)year);
MoneyOut.println(" Old Loan ", (int)balance);
// produce output: current year, old loan,
MoneyOut.println(" Loans plus 3% interest ", newlinterest);
// loan plus 3% interest, wages,
MoneyOut.println(" Wages ", (int)newwages);
MoneyOut.println(" Wages minus threshold ", wagest);
//wages minus 10,000 pounds, repayment, new loan
MoneyOut.println(" Repayment ", (int)repaid);
MoneyOut.println(" New loan amount ", newbalance);
if (year >=30)
{
System.out.println(0);
} // 'break' out of the 'while' loop if more than 30 years have passed
} // end of the 'while' loop, program leaves it when loan is 0 or less
System.out.println(" It took you " +(int)year +
" years to repay the loan ");
// print out the number of year it took to repay the loan
System.out.println(" The orginal loan was " + "£" + loans +
" but you had to pay back " + yrbal);
// print out the original loan and the total amount paid back
} // end of main
} // end of class LoanPayments
The Money.out Class is part of the Program also I disable the If Statement Greyed out to see if that was causing the issue but it still won't Implement as required.