Separate two loops in java -


I must write a program that reads the balance and interest rate, and in 10 years the account value is calculated simultaneously, monthly Displays and daily compounds

I have written for the annual agreement and monthly. In the second loop for the monthly rate, the program reads the value of "balance" after compounding yearly, whereas I need to read the primary value. How is it possible to separate the two loops, so they do not affect each other? Here's my code:

  import java.util.Scanner; Public square BankInterest {public static zero major (string [] args) {System.out.println ("Please enter your balance:"); Scanner Keyboard = New Scanner (System.in); Double balance = keyboard.nextDouble (); Int year = 0; Int month = 0; Int day = 0; System.out.println ("Please enter the annual interest rate in decimal form:"); Double interest rate = keyboard.nextDouble (); While (years and lieutenant; 10) {double interest = balance * interest rate; Balance = balance + interest; Year ++; } System.out.println ("Balance after 10 years with annual interest" + remaining); While (months and lieutenant; 120) {double interest = balance * interest rate / 12; Balance = balance + interest; Months ++; } System.out.println ("Balance after 10 years with monthly interest rate" + Balance); }}   

When the program is running and I perform the input 100 and interest rate for the balance, the annual cyclone works and displays: 10 years later the balance with an annual interest of 121.89944199947573

And the second loop balances and displays this value: With the monthly interest rate, the balance amount is 148.86352955791543

However, if my code was correct, Number should display: 122.119 9 43386

You can place both ends in different functions and pass both equations and interesRate as arguments, like below.

  Import java.util.Scanner; Public Class Bank Interest {Static Public Double Annual Interest (Double Balance, Double Interest Rate) {Ent Year = 0; While (years and lieutenant; 10) {double interest = balance * interest rate; Balance = balance + interest; Year ++; } Refund balance; } Fixed public double monthly list (double balance, double interest rate) {int month = 0; While (months and lieutenant; 120) {double interest = balance * interest rate / 12; Balance = balance + interest; Months ++; } Refund balance; } Public static zero main (string agr []) {scanner keyboard = new scanner (System.in); System.out.println ("Please enter your balance:"); Double balance = keyboard.nextDouble (); System.out.println ("Please enter the annual interest rate in decimal form:"); Double interest rate = keyboard.nextDouble (); System.out.println ("Balance after 10 years with annual interest" + annual interest (balance, interest rate)); System.out.println ("Balance after 10 Years with Monthly Interest Rate" + Monthly Interest (Balance, Interest Rate)); }}    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -