How to do math with variables!
If you already know how to declare variables and set values to them, setting up an equation for your program to run is very simple!
Here's an excerpt from my code in the video:
package mathpractice;
public class Mathpractice {
public static void main(String[] args) {
int loanAmount;
int amountOfYears;
double annualRate;
double profitGained;
loanAmount = 5000;
amountOfYears = 15;
annualRate = 6.0;
profitGained = loanAmount * (annualRate/100) * amountOfYears;
System.out.println("With the interest rate being: "+annualRate+"%");
System.out.println("The amount of years is: "+amountOfYears);
System.out.println("And the loan amount being: $" +loanAmount);
System.out.println("Teh amount of interest gained is: $"+profitGained);
}
}
Challenge:
If you already understand all of that, try out for some extra credit and do some formatting on the above code using the DecimalFormat class!
In this code, I loaned a friend $5,000 and he paid me back over the course of 15 years at a rate of 6% interest per year. I needed to know how much money I made off of him!
DETAILED VIDEO BELOW
No comments:
Post a Comment