简体   繁体   English

重复值

[英]Repeating values

I have a project that has to ask the user for a credit balance and a rate, and then output how much you would have to pay for the next 12 months if you payed the mininum payment(calculated by: balance * 0.03, and if thats lower than 10 then the mininum payment is $10). 我有一个项目,要求用户提供贷方余额和利率,然后输出如果您支付了最低还款额(计算方式为:余额* 0.03,并且如果那是低于10,则最低付款金额为10美元)。 I am cannot figure out why "balance * 0.03" will not increase in value of "minPay" as it loops. 我无法弄清楚为什么“ balance * 0.03”在循环时不会增加“ minPay”的值。

import java.util.Scanner;
public class Project1Term2 
{
public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);

    System.out.println();
    System.out.print("Enter credit card balance: ");
    double balance = in.nextDouble();
    System.out.print("Enter interest rate: ");
    double interest = in.nextDouble();

    interest = interest / 100;

    for(int i = 0; i <= 12; i++)
    {
        Calculate out = new Calculate(balance, interest);
        System.out.println(out.getNewValue());
        balance = out.getNewValue();
    }   
}  
}

class Calculate
{
private final double rate;
private double balance;
private double minPay;
private double newBalance;

Calculate(double balance, double rate)
{
    this.balance = balance;
    this.rate = rate;
}

double getNewValue()
{
    if((balance * 0.03) < 10)
    {
        minPay = 10;
    }
    else
    {
        minPay = balance * 0.03;
    }
    newBalance = (balance - minPay) + (balance * rate);

    return newBalance;
}
}

You are creating a new instance of Calculate on every loop. 您将在每个循环上创建一个新的Calculate实例。 That´s wrong on many ways. 这在很多方面都是错误的。 One consequence is that you´re never incrementing minPay , as each instance is used only once. 结果是您永远不会增加minPay ,因为每个实例仅使用一次。

If you are simply looking for an answer as to why your balance wasn't decreasing, the answer is that your logic was incorrect. 如果您只是在寻找有关为什么余额没有减少的答案,那么答案是您的逻辑不正确。

newBalance = (balance - minPay) + (balance * rate);

Should be: 应该:

newBalance = balance + (balance * rate) - minPay ;

You want to add the interest to the current balance, and then subtract whatever the minimum payment is. 您想将利息添加到当前余额中,然后减去最低付款额。 Running your program with that logic gave me: 以这种逻辑运行您的程序给了我:

Enter credit card balance: 100
Enter interest rate: .1
90.1
80.1901
70.2702901
60.340560390099995
50.4009009504901
40.45130185144059
30.49175315329203
20.522244906445323
10.542767151351768
0.5533099185031194
-9.446136771578377
-19.455582908349953
-29.475038491258303

You could also work on some better coding practices. 您还可以进行一些更好的编码实践。 For example, you shouldn't instantiate a new instance of the class every loop. 例如,您不应在每个循环中实例化该类的新实例。 Instead, continue to use the same class instance. 而是继续使用相同的类实例。 You should also check to see if the balance is 0, for example the output I got from running your program allowed me to pay into the negatives. 您还应该检查一下余额是否为0,例如,我从运行程序时得到的输出使我可以支付负数。

Hope this helps, feel free to ask questions if it doesn't make sense. 希望这会有所帮助,如果没有意义,请随时提出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM