I'm currently learning the Java language, which is my first programming language. I did research into arrays and was trying to make my program more efficient and simple with them. I realize there is a lot of repetitions in the original code. I appreciate any tips and criticism.
import java.util.Scanner;
public class Calculator {
static Scanner sc = new Scanner(System.in);//sets up scanner import
public static void main(String[] args)
{
double initalInvestment;
double interestRate;
int yearsInvested;
int arrInt[] = new int[5];
arrInt[0] = 1;
int i = 1;
while (i <= 4) {
arrInt[i] = i + 1;
i++;
}
System.out.println("Enter Inital Investment: ");
initalInvestment = sc.nextDouble();
System.out.println("Enter Interest Rate: ");
interestRate = sc.nextDouble();
System.out.println("Enter Years Invested: ");
yearsInvested = sc.nextInt();
double money1 = initalInvestment * interestRate;
double money2 = money1 + initalInvestment;
double y1 = money2;
double y2 = y1 * interestRate + y1;
double y3 = y2 * interestRate + y2;
if (yearsInvested <= 1)
{
System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
} else if (yearsInvested <= 2) {
System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
} else if (yearsInvested <= 3) {
System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
System.out.println("Your Total for year: " + arrInt[2] + ", is " + y3 + "$");
} else if (yearsInvested <= 4) {
System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
System.out.println("Your Total for year: " + arrInt[2] + ", is " + y3 + "$");
System.out.println("Your Total for year: " + arrInt[3] + ", is " + y3 + "$");
} else if (yearsInvested <= 5) {
System.out.println("Your Total for year: " + arrInt[0] + ", is " + y1 + "$");
System.out.println("Your Total for year: " + arrInt[1] + ", is " + y2 + "$");
System.out.println("Your Total for year: " + arrInt[2] + ", is " + y3 + "$");
System.out.println("Your Total for year: " + arrInt[3] + ", is " + y3 + "$");
}
}
}