Java - 需要帮助使我的计算程序更高效,使用数组?

时间:2017-10-24 01:54:08

标签: java arrays performance

我目前正在学习Java语言,这是我的第一门编程语言。我对数组进行了研究,并试图使我的程序更加高效和简单。我意识到原始代码中有很多重复。我感谢任何提示和批评。

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 + "$");
    }

 }

}

2 个答案:

答案 0 :(得分:1)

您不希望yearsInvested的每个可能值都有明确的案例。这种方法意味着您可能会轻微修改复制粘贴代码,这很容易出错 - 事实上,我看到了复制粘贴错误;您的5年案例仅为4年,{4}案例中列出y3两次。 (并想象这样做了241年,试图计算乔治华盛顿银行账户目前在1776年以后单独留下的金额 - 这将是一个不合理的代码量。)

现在,您的数组内容只是{1,2,3,4,5}。你当然不需要数组到五。我不相信这个程序需要一个数组(你可能只需要在一个for循环中完成输入和输出),但是如果你想使用一个,那么放置它可能是有意义的你的计算结果。

您可以使用for循环将数据放入数组中并从数组中读取值 - 例如,从数组中读取值可能如下所示:

for (int i = 0; i < yearsInvested; x++)
{
    System.out.println("Your Total for year: " + (i+1) + ", is " + resultsArray[i] + "$");
}

答案 1 :(得分:0)

你根本不需要存储总数 - 如果您所做的只是打印出每年的累计总数,那么您可以随时计算:

List<Integer> totals = new ArrayList<>();
for (int year = 0; year < yearsInvested; year++) {
    investment += investment * interestRate;
    totals.add(investment);
}

如果您确实需要存储值,那么我建议您使用{{1}}而不是数组。对于初学者来说,它们更容易学习。

{{1}}