如何重置变量但不更改旧值?

huangapple go评论59阅读模式
英文:

How to reset variable but not changing the old value?

问题

每次循环时,我的 interestRate 变量没有重置。我希望它在循环时更新我的利息,在开始时我有正确的值 1650,但是当到达第三个输入时,第一、二、三个的利息将会累加。我期望输出为 1760.0,但它显示为 1925.0。

import java.util.*;
class SavingsAccount {

    private double balance;
    public static double interestRate = 0;
    static double updatedrate;
    public static void setInterestRate(double newRate){
        interestRate = newRate;
    }
    public static double getInterestRate(){
        return interestRate;
    }
    public double getBalance(){
        return balance;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public double withdraw(double amount){
        if(balance >= amount){
            balance = balance - amount;
        }
        else{
            amount = 0;
        }
        return amount;
    }
    public void addInterest(){
        double interest = balance * getInterestRate(); 
        balance = balance + interest;
    }
    public static void showBalance(SavingsAccount account){
        System.out.println("Your new Balance is " + account.getBalance());
    }
}

public class RunSavingsAccount {
    public static void main(String[] args) {
        SavingsAccount savings = new SavingsAccount();
        Scanner sc = new Scanner(System.in);
        try{
            System.out.print("Enter Interest Rate: ");
            double rate1 = sc.nextDouble();
            savings.setInterestRate(rate1);
            System.out.print("Enter Deposit Amount: ");
            double balance1 = sc.nextDouble();
            savings.deposit(balance1);
            System.out.println("Your balance is: " + balance1);
            boolean bol = false;
            while(!bol){
                System.out.print("Press D for another deposit or W to withdraw or E to exit: ");
                String transaction = sc.next();
                if (transaction.equals("d")) {
                    System.out.print("Enter Deposit Amount: ");
                    double balance2 = sc.nextDouble();
                    savings.deposit(balance2);
                    savings.addInterest();
                    savings.showBalance(savings);  
                }   
                else if(transaction.equals("w")){
                    System.out.print("Enter Withdraw Amount: ");
                    double with = sc.nextDouble();
                    savings.withdraw(with);
                    System.out.println();
                    System.out.println("Your have successfully withdrawn: " + with);
                    savings.showBalance(savings);
                }
                else if(transaction.equals("e")){
                    System.out.println("Thank You Please Come Again.");
                    bol = !bol;
                }
                else{
                    System.out.println("Invalid Input");
                }
            }
        }
        catch(Exception e){
            System.out.print("Invalid Input");
        }
    }
}

输出:

Enter Interest Rate: 0.10
Enter Deposit Amount: 500
Your balance is: 500.0
Press D for another deposit or W to withdraw or E to exit: d
Enter Deposit Amount: 1000
Your new Balance is 1650.0
Press D for another deposit or W to withdraw or E to exit: d
Enter Deposit Amount: 100
Your new Balance is 1760.0
Press D for another deposit or W to withdraw or E to exit:
英文:

Every time I loop my interestRate variable is not resetting. I want it to update my interest when looping, at first I have the correct value of 1650 but when it comes to the third input all the interest from first, second, and third will just sum up. I'm expecting 1760.0 as the output but it shows 1925.0.

import java.util.*;
class SavingsAccount {
private double balance;
public static double interestRate = 0;
static double updatedrate;
public static void setInterestRate(double newRate){
interestRate = newRate;
}
public static double getInterestRate(){
return interestRate;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance=balance + amount;
}
public double withdraw(double amount){
if(balance>=amount){
balance= balance-amount;
}
else{
amount=0;
}return amount;
}
public void addInterest(){
double interest = balance * getInterestRate(); 
balance = balance + interest;
}
public static void showBalance(SavingsAccount account){
System.out.println("Your new Balance is "+ account.getBalance());		
}
public class RunSavingsAccount {
public static void main(String[] args) {
SavingsAccount savings = new SavingsAccount();
Scanner sc = new Scanner(System.in);
try{
System.out.print("Enter Interest Rate: " );
double rate1 = sc.nextDouble();
savings.setInterestRate(rate1);
System.out.print("Enter Deposit Amount: " );
double balance1 = sc.nextDouble();
savings.deposit(balance1);
System.out.println("Your balance is: "+ balance1);
boolean bol = false;
while(!bol){
System.out.print("Press D for another deposit or W to withdraw or E to exit: ");
String transaction = sc.next();
if (transaction.equals("d")) {
System.out.print("Enter Deposit Amount: " );
double balance2 = sc.nextDouble();
savings.deposit(balance2);
savings.addInterest();
savings.showBalance(savings);  
}	
else if(transaction.equals("w")){
System.out.print("Enter Withdraw Amount: " );
double with = sc.nextDouble();
savings.withdraw(with);
System.out.println("");
System.out.println("Your have succesfully withdraw: "+ with);
savings.showBalance(savings);
}
else if(transaction.equals("e")){
System.out.println("Thank You Please Come Again.");
bol = !bol;
}
else{
System.out.println("Invalid Input");
}
}
}
catch(Exception e){
System.out.print("Invalid Input");
}
}
}

Output:

Enter Interest Rate: .10
Enter Deposit Amount: 500
Your balance is: 500.0
Press D for another deposit or W to withdraw or E to exit: d
Enter Deposit Amount: 1000
Your new Balance is 1650.0
Press D for another deposit or W to withdraw or E to exit: d
Enter Deposit Amount: 100
Your new Balance is 1925.0
Press D for another deposit or W to withdraw or E to exit:

答案1

得分: 0

你在每次存款后都对整个余额添加利息:

            savings.deposit(balance2);
savings.addInterest();

从技术上讲,如果您每年进行一次存款,那么这样做是正确的。

我不确定您在尝试模拟什么。也许您应该有一个单独的命令,在每年末或类似的时间点仅添加一次利息。

        if (transaction.equals("i")) {
savings.addInterest();
savings.showBalance(savings);  
}
else if (transaction.equals("d")) {
System.out.print("输入存款金额:");
double balance2 = sc.nextDouble();
savings.deposit(balance2);
savings.showBalance(savings);  
}
英文:

You're adding interest on the whole balance after every deposit:

            savings.deposit(balance2);
savings.addInterest();

Technically that would be correct if you do a deposit every year.

I'm not sure what you're trying to simulate here. Maybe you should have a separate command for adding interest only once at the end of the year or so.

        if (transaction.equals("i")) {
savings.addInterest();
savings.showBalance(savings);  
}
else if (transaction.equals("d")) {
System.out.print("Enter Deposit Amount: " );
double balance2 = sc.nextDouble();
savings.deposit(balance2);
savings.showBalance(savings);  
}   

答案2

得分: 0

以下是翻译好的内容:

该应用程序正好按照预期执行。

public void deposit(double amount){
  balance=balance + amount;
}

public void addInterest(){
  double interest = balance * getInterestRate(); 
  balance = balance + interest;
}

您总是先添加存款,然后再添加利息,因此:

余额:500
addDeposit(1000) => 余额 1500
addInterest => 余额 = 1500 + (1500 * 0.1) = 1650
addDeposit(100) => 余额 = 1650 + 100 = 1750
addInterest() => 余额 = 1750 + (1750 * 0.1) = 1750 + 175 = 1925

我猜您的意思不是要对全额余额计算利息,或者您可能做了其他不同的假设。

通常,我建议您获取一个带有调试器的集成开发环境(IDE),以便逐步执行代码,这样您可以更深入地了解发生了什么情况。

英文:

The application does exactly what it is supposed to do.

public void deposit(double amount){
balance=balance + amount;
}
public void addInterest(){
double interest = balance * getInterestRate(); 
balance = balance + interest;
}

You always first add the deposit and then addInterest,so

balance: 500  
addDeposit(1000) => balance 1500  
addInterest      => balance = 1500 + (1500 * 0,1) = 1650  
addDeposit(100)  => balance = 1650 + 100 = 1750  
addInterest()    => balance = 1750 + (1750 * 0,1) = 1750 + 175 = 1925

I guess you do not meant to have the interest on the full balance sum, or you made some other assumption which is different.

Generally, I would recommend you to get an IDE with a debugger that allows step-by-step execution of the code so you can get more insight on what is happening.

huangapple
  • 本文由 发表于 2020年10月5日 19:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64207453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定