savingsaccount程序

jtw3ybtb  于 2021-07-13  发布在  Java
关注(0)|答案(4)|浏览(256)

我有一点小麻烦与这个程序,即使它编译。它说我必须添加一个main方法,但是,我这样做了,结果得到了21个错误。有人请帮帮我。。
创建一个类savingsaccount。使用静态类变量存储每个存储程序的annualInterestate。类的每个对象都包含一个私有示例变量savingsbalance,该变量指示储蓄者当前的存款金额。提供计算月利息的方法,用余额乘以年利息除以12计算月利息;这个利息应该加到储蓄余额上。提供一个静态方法modifyinterestrate,将annualinterestrate设置为新值。编写一个驱动程序来测试类savingsaccount。示例化两个不同的savingsaccount对象saver1和saver2,余额分别为$2000.00和$4000.00。将AnnualInterestate设置为3%,然后计算每月利息并打印每个储户的新余额。然后将annualinterestate设置为5%,计算下个月的利息并打印每个储户的新余额。

import java.util.Scanner;
public class SavingsAccount{

    private static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount()
    {
        savingsBalance = 0;
        annualInterestRate = 0;
    }

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
        annualInterestRate = 0;
    }

    public void calculateMonthlyInterest()
    {
        System.out.println("Current savings balance: " + savingsBalance);
        double monthlyInterest;
        monthlyInterest = (savingsBalance * annualInterestRate)/12;
        savingsBalance = monthlyInterest;
        System.out.println("New savings balance: " + savingsBalance);
    }

    public double getBalance()
    {
        return savingsBalance;
    }
    public static void modifyInterestRate(double newInterestRate)
    {
        annualInterestRate = newInterestRate;
    }
}
class Driver
{
    public static void main(String[] args)
    {
        SavingsAccount saver1 = new SavingsAccount(2000);
        SavingsAccount saver2 = new SavingsAccount(4000);
        saver1.modifyInterestRate(.03);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.03);
        saver2.calculateMonthlyInterest();
        saver1.modifyInterestRate(.05);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.05);
        saver2.calculateMonthlyInterest();
    }
}
fd3cxomn

fd3cxomn1#

你的代码运行得很好。请另存为 SavingsAccount.java 以及 compile 使用 javac SavingsAccount.java 以及 run 使用 java Driver .
compile 以及 run 你的代码和 output 是这个吗

n3h0vuf2

n3h0vuf22#

public class SavingsAccount{

private static double annualInterestRate;
private double savingsBalance;

public SavingsAccount()
{
    savingsBalance = 0;
    annualInterestRate = 0;
}

public SavingsAccount(double balance)
{
    savingsBalance = balance;
    annualInterestRate = 0;
}

public void calculateMonthlyInterest()
{
    System.out.println("Current savings balance: " + savingsBalance);
    double monthlyInterest;
    monthlyInterest = (savingsBalance * annualInterestRate)/12;
    savingsBalance += monthlyInterest;
    System.out.println("New savings balance: " + savingsBalance);
}

public double getBalance()
{
    return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
    annualInterestRate = newInterestRate;
}

}

sulc1iza

sulc1iza3#

您可以尝试以下代码。

package savingsaccount;

public class SavingsAccount{

    private static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount()
    {
        savingsBalance = 0;
        annualInterestRate = 0;
    }

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
        annualInterestRate = 0;
    }

    public void calculateMonthlyInterest()
    {
        System.out.println("Current savings balance: " + savingsBalance);
        double monthlyInterest;
        monthlyInterest = (savingsBalance * annualInterestRate)/12;
        savingsBalance = monthlyInterest;
        System.out.println("New savings balance: " + savingsBalance);
    }

    public double getBalance()
    {
        return savingsBalance;
    }
    public static void modifyInterestRate(double newInterestRate)
    {
        annualInterestRate = newInterestRate;
    }

    public static void main(String[] args)
    {
        SavingsAccount saver1 = new SavingsAccount(2000);
        SavingsAccount saver2 = new SavingsAccount(4000);
        SavingsAccount.modifyInterestRate(.03);
        saver1.calculateMonthlyInterest();
        SavingsAccount.modifyInterestRate(.03);
        saver2.calculateMonthlyInterest();
        SavingsAccount.modifyInterestRate(.05);
        saver1.calculateMonthlyInterest();
        SavingsAccount.modifyInterestRate(.05);
        saver2.calculateMonthlyInterest();
    }
}
taor4pac

taor4pac4#

将main方法放在savingsaccount类中。
这样地,

public class SavingsAccount{

   private static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount()
    {
        savingsBalance = 0;
        annualInterestRate = 0;
    }

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
        annualInterestRate = 0;
    }

    public void calculateMonthlyInterest()
    {
        System.out.println("Current savings balance: " + savingsBalance);
        double monthlyInterest;
        monthlyInterest = (savingsBalance * annualInterestRate)/12;
        savingsBalance = monthlyInterest;
        System.out.println("New savings balance: " + savingsBalance);
    }

    public double getBalance()
    {
        return savingsBalance;
    }
    public static void modifyInterestRate(double newInterestRate)
    {
        annualInterestRate = newInterestRate;
    }

    public static void main(String[] args)
    {
        SavingsAccount saver1 = new SavingsAccount(2000);
        SavingsAccount saver2 = new SavingsAccount(4000);
        saver1.modifyInterestRate(.03);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.03);
        saver2.calculateMonthlyInterest();
        saver1.modifyInterestRate(.05);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.05);
        saver2.calculateMonthlyInterest();
    }
}

相关问题