Пересмотренная процентная ставка по массиву: - PullRequest
0 голосов
/ 09 мая 2020

Я обнаружил ошибку «ошибка: неверные типы операндов для двоичного оператора '*'» для interestEarned = salary * interestRate;

Я обнаружил аналогичную ошибку с multiplicator = 1 + interestRate;

Другая ошибка, с которой я столкнулся: «/Project3Revised.java:87: error: double не может быть разыменовано» для for (int i; i < interestEarned.length; i++) { и та же проблема для System.out.println("Year: " + year + " Interest Earned: " + interestEarned.get[i] + " Balance: " + balance);

Я не знаю, как решить эти проблемы. Вот что я хочу увидеть в примере компиляции моей программы:

Enter input: salary savings_rate years_employed name(last,first)

The user inputs > 70000 .10 30 smith john

Salary: 70000.0

Savings rate:0.1

Interest rate:0.0

Years of Employment: 30.0

Last Name: smith

First Name: john

Year: 1 Rate: 0.06  Interest Earned: 420.00 Balance: 7420.00

Year: 2 Rate: 0.06  Interest Earned: 865.20 Balance: 15285.20

Year: 3 Rate: 0.06  Interest Earned: 1337.11 Balance: 23622.31

Year: 4 Rate: 0.06  Interest Earned: 1837.34 Balance: 32459.65

Year: 5 Rate: 0.06  Interest Earned: 2367.58 Balance: 41827.23

Year: 6 Rate: 0.02  Interest Earned: 976.54 Balance: 49803.77

Year: 7 Rate: 0.02  Interest Earned: 1136.08 Balance: 57939.85

Year: 8 Rate: 0.02  Interest Earned: 1298.80 Balance: 66238.65

Year: 9 Rate: 0.02  Interest Earned: 1464.77 Balance: 74703.42

Year: 10 Rate: 0.02  Interest Earned: 1634.07 Balance: 83337.49

Year: 11 Rate: 0.15  Interest Earned: 13550.62 Balance: 103888.11

Year: 12 Rate: 0.15  Interest Earned: 16633.22 Balance: 127521.33

Year: 13 Rate: 0.15  Interest Earned: 20178.20 Balance: 154699.53

Year: 14 Rate: 0.15  Interest Earned: 24254.93 Balance: 185954.46

Year: 15 Rate: 0.15  Interest Earned: 28943.17 Balance: 221897.62

Year: 16 Rate: 0.00  Interest Earned: 0.00 Balance: 228897.62

Year: 17 Rate: 0.00  Interest Earned: 0.00 Balance: 235897.62

Year: 18 Rate: 0.00  Interest Earned: 0.00 Balance: 242897.62

Year: 19 Rate: 0.30  Interest Earned: 74969.29 Balance: 324866.91

Year: 20 Rate: 0.30  Interest Earned: 99560.07 Balance: 431426.99

Year: 21 Rate: -0.04  Interest Earned: -17537.08 Balance: 420889.91

Year: 22 Rate: -0.04  Interest Earned: -17115.60 Balance: 410774.31

Year: 23 Rate: -0.04  Interest Earned: -16710.97 Balance: 401063.34

Year: 24 Rate: -0.04  Interest Earned: -16322.53 Balance: 391740.80

Year: 25 Rate: -0.04  Interest Earned: -15949.63 Balance: 382791.17

Year: 26 Rate: 0.03  Interest Earned: 11693.74 Balance: 401484.91

Year: 27 Rate: 0.03  Interest Earned: 12254.55 Balance: 420739.45

Year: 28 Rate: 0.03  Interest Earned: 12832.18 Balance: 440571.64

Year: 29 Rate: 0.03  Interest Earned: 13427.15 Balance: 460998.79

Year: 30 Rate: 0.03  Interest Earned: 14039.96 Balance: 482038.75 

Вот основной код моей программы.

import java.util.Scanner;

public class Project3Revised {
public static void main(String[] args) {

    Scanner scnr = new Scanner(System.in);

    int salary;
    double savingsRate;
    double employmentYears;
    double retirementSavings;


    System.out.println("Enter input salary savings_rate years_employed lastname firstname");

    salary = scnr.nextInt();
    savingsRate = scnr.nextDouble();
    employmentYears = scnr.nextDouble();
    String l = scnr.next(); // Last Name
    String f = scnr.next(); // First Name
    double[] interestRate = { 0,.06,.06,.06,.06,.06,.02,.02,.02,.02,.02,.15,.15,.15,.15,.15,0,0,0,.30,.30,-.04,-.04,-.04,-.04,-.04,.03,.03,.03,.03,.03 };

    if (salary < 0) {
        System.out.println("Salary is negative");
    }
    else if (salary > 1000000) {
        System.out.println("Error: Salary " + salary + "exceeds maximum 1000000.0");
    }
    else {
        System.out.println("Salary: " + salary);
    }

    if (savingsRate < 0) {
        System.out.println("Savings rate is negative");
    }
    else if (savingsRate > 0.5) {
        System.out.println("Error: Savings rate " + savingsRate + "exceeds maximum 0.5");
    }
    else {
        System.out.println("Savings Rate:" + savingsRate);
    }

    if (employmentYears < 0) {
        System.out.println("Years employed is negative!");
    }
    else if (employmentYears > 50) {
        System.out.println("Error: Years employed " + employmentYears + "exceeds maximum 50");
    }
    else {
        System.out.println("Years of Employment: " + employmentYears);
    }

    if (l.length() == 0 || l.length() == 1) {
       System.out.println("Error: Name too short");
    }
    else if (l.equals(f)) {
        System.out.println("Error: Last Name and First Name are the same");
    }
    else {
        System.out.println("Last Name: " + l);
    }

    if (f.length() == 0 || f.length() == 1) {
        System.out.println("Error: Name too short");
    }
    else if (f.equals(l)) {
        System.out.println("Error: Last Name and First Name are the same");
    }
    else {
        System.out.println("First Name: " + f);
    }

    System.out.println(" ");
    System.out.println(" ");

    int year;
    double interestEarned;
    double balance; 
    double multiplicator; // Interest earned plus an increment

    year = 0;
    interestEarned = salary * interestRate;
    balance = salary * savingsRate + interestEarned;
    multiplicator = 1 + interestRate;

    for (int i; i < interestEarned.length; i++) {
        ++year;
        System.out.println("Year: " + year + " Interest Earned: " + interestEarned.get[i] + " Balance: " + balance);

    }


  }
}

1 Ответ

0 голосов
/ 13 мая 2020

Для ошибки double cannot be dereferenced:

interestEarned - это отдельное числовое c значение (a double), но вы пытаетесь использовать его как массив.

Для ошибки bad operand types возникает противоположная проблема: interestRate - это массив, но вы пытаетесь использовать его как отдельное числовое c значение.

Это своего рода смешивание отдельных значения и массивы могут работать в чем-то вроде numpy / pandas в Python, но это не работает в Java.

Я не уверен на 100%, что должен делать код , но вам придется выполнить математические вычисления для отдельных элементов массива (возможно, в al oop) и убедиться, что вы не смешиваете отдельные числа с массивами.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...