在java中分开两个循环

时间:2014-04-22 12:43:23

标签: java loops

我应该编写一个读取余额和利率的程序,并在十年内显示每年,每月和每日化合物的帐户价值。

我写过每年的复合和月刊。在每月费率的第二个循环中,程序在每年复合后读取“余额”的值,而我需要它读取主要值。如何将两个循环分开,这样它们就不会相互影响?这是我的代码:

import java.util.Scanner;
public class BankInterest {
    public static void main(String []args) {
        System.out.println("Please enter your balance: ");
        Scanner keyboard = new Scanner(System.in);
        double balance = keyboard.nextDouble();
        int years = 0;
        int months = 0;
        int days = 0;
        System.out.println("Please enter the ann1ual interest rate in decimal form: ");
        double interestRate = keyboard.nextDouble();

        while (years<10) {
            double interest = balance * interestRate;
            balance = balance + interest;
            years++;

        }
        System.out.println("Balance after 10 years with annual interest is " + balance);

        while (months<120) {
            double interest = balance * interestRate/12;
            balance = balance + interest;
            months++;
        }
        System.out.println("Balance after 10 years with monthly interest rate is " + balance);
    }

}

当程序运行时我输入100作为余额,0.02输入利率,每年复合效果很好并显示: 10年后的余额,年利率为121.89944199947573

第二个循环将此值视为余额并显示: 10年后的月平均利率为148.86352955791543

虽然如果我的代码是正确的,它应该显示这个数字:122.119943386

2 个答案:

答案 0 :(得分:1)

您可以将两个循环放在不同的函数中,并将balance和interesRate作为参数传递,如下所示。

import java.util.Scanner;

public class BankInterest {
    static public double annualInterest(double balance, double interestRate) {
         int years = 0;
         while (years < 10) {
            double interest = balance * interestRate;
            balance = balance + interest;
            years++;
        }

        return balance;
    }

    static public double monthlyInterest(double balance, double interestRate) {
      int months = 0;
         while (months < 120) {
            double interest = balance * interestRate/12;
            balance = balance + interest;
            months++;
        }

        return balance;
    }

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter your balance: ");
        double balance = keyboard.nextDouble();
        System.out.println("Please enter the ann1ual interest rate in decimal form: ");
        double interestRate = keyboard.nextDouble();

        System.out.println("Balance after 10 years with annual interest is " + 
        annualInterest(balance, interestRate));
        System.out.println("Balance after 10 years with monthly interest rate is " + 
        monthlyInterest(balance, interestRate));
    }
}

答案 1 :(得分:0)

您需要为初始余额和更新余额声明两个不同的变量,以便您可以在两个循环中访问原始余额。我已在下面展示了这一点。

public static void main(String []args) {
System.out.println("Please enter your balance: ");
Scanner keyboard = new Scanner(System.in);
double startingBalance = keyboard.nextDouble();
double finalBalance=0;
int years = 0;
int months = 0;
int days = 0;
System.out.println("Please enter the ann1ual interest rate in decimal form: ");
double interestRateYearly = keyboard.nextDouble();
double interstRateMonthly = interestRateYearly;
double interstRateDayliy = interestRateYearly;

while (years<10) {
    double interest = startingBalance * interestRateYearly ;
    finalBalance = startingBalance + interest;
    years++;

}
System.out.println("Balance after 10 years with annual interest is " + finalBalance);

while (months<120) {
    double interest = startingBalance * interstRateMonthly /12;
    finalBalance = startingBalance + interest;
    months++;
}
System.out.println("Balance after 10 years with monthly interest rate is " +     finalBalance);

}

}