简体   繁体   English

Java新手逻辑

[英]Novice logic in Java

I'm learning java from books and I've come across a logic problem, I know the code isn't as effective as it could be but I want to understand the problem so I can better understand how Java works and avoid more complex issues in the future. 我正在从书本中学习Java,并且遇到了逻辑问题,我知道代码没有达到应有的效果,但是我想了解这个问题,以便更好地了解Java的工作原理并避免更复杂的问题在将来。

The program I'm trying to write is supposed to read in account balance and an interest rate then give the balance after a year and two years. 我要编写的程序应该读入帐户余额和利率,然后在一年零两年后给出余额。

The interest rate for the second year should be calculated on the total from the first year. 第二年的利率应从第一年的总和中计算得出。

But my program is just adding the same amount of interest from the first year onto the second year. 但是我的计划是从第一年到第二年增加相同的利息。 So in the case that the balance is 6000 and the interest is 4.25, I'm getting 6255.0 for the first year and 6510.0 for the second. 因此,在余额为6000且利息为4.25的情况下,第一年的收入为6255.0,第二年的收入为6510.0。 I should be getting 6520.83 for the second year total cause the interest for the first year should be earning calculated interest too. 第二年总计我应该得到6520.83,因为第一年的利息也应该获得计算的利息。

import acm.program.*;

public class BalanceAndIntrest extends ConsoleProgram {

    public void run() {

        println("This program calculates intrest.");
        double balance = readDouble("Enter your balance here: ");
        double intrest = readDouble("Enter your intrest rate here: ");
        double yearsIntrest = (balance / 100) * intrest;
        balance += yearsIntrest;
        println("The balance after a year would be £" + balance +".");
        balance += yearsIntrest;
        println("The balance after two years would be £" + balance +".");

my logic here is that 我的逻辑是

it reads in the balance it reads in the interest years interest is defined by dividing the balance by 100 then multiplying that by the interest rate. 它读入利息年中的余额,将余额除以100,然后再乘以利率来定义利息。 then interest rate is added to balance then I add the interest rate again, which should give a different interest rate, seeing as the value of balance has changed at this point, but it doesn't, it just calculates interest on the number that is read in rather than the updated balance. 然后将利率添加到余额中,然后再次添加利率,这应该给出不同的利率,因为此时余额的值已更改,但实际上并没有,它只是计算读入而不是更新后的余额。

Why is this? 为什么是这样?

I thought that by the end of the program the value of balance should be the updated value so the years interest var should work.., but obviously I'm getting something wrong. 我以为,到程序结束时,余额的值应该是更新的值,以便年利率var应该可以工作..但是,显然我弄错了。

If your write a statement like 如果你写一个像

double yearsInterest = (balance / 100) * interest;

you don't define what interest means in a mathematical sense. 您没有从数学意义上定义兴趣的含义。 What you actually do is to calculate the interest with the values currently referenced by balance and interest . 您实际要做的是使用当前由balanceinterest引用的值来计算interest If you want to define this simply add a method 如果要定义它,只需添加一个方法

private double calculateInterest(double balance, double interest) { 
  return (balance / 100) * interest;
}

and use it like this 像这样使用

balance += calculateInterest(balance, interest);
println("The balance after a year would be £" + balance +".");
balance += calculateInterest(balance, interest);
println("The balance after two years would be £" + balance +".");

You need to re-calculate the interest for the second year eg like this: 您需要重新计算第二年的利息,例如:

println("This program calculates intrest.");
double balance = readDouble("Enter your balance here: ");
double intrest = readDouble("Enter your intrest rate here: ");
double firstYearIntrest = (balance / 100) * intrest;
balance += firstYearIntrest;
println("The balance after a year would be £" + balance +".");
double secondYearIntrest = (balance / 100) * intrest;
balance += secondYearIntrest;
println("The balance after two years would be £" + balance +".");

(Also for when you finished learning Java: later dont use float/double for money, always either use an arbitrary-precision decimal integer or the longest integer your language has and represent fractions of cent's) (同样,当您结束学习Java时:以后不要为了金钱而使用float / double,请始终使用任意精度的十进制整数或您的语言具有的最长整数,并且代表分的小数)

you didn't recompute the interest based on the updated balance, after the first year: 第一年后,您没有根据更新后的余额重新计算利息:

double balance = readDouble("Enter your balance here: ");
double intrest = readDouble("Enter your intrest rate here: ");
double yearsIntrest = (balance / 100) * intrest;
balance += yearsIntrest;
println("The balance after a year would be £" + balance +".");

// Now the interest must be recomputed, since the balance has changed:
yearsIntrest = (balance / 100) * intrest;

balance += yearsIntrest;
println("The balance after two years would be £" + balance +".");

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

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