(Java初学者)-遇到程序问题



好的,所以我正在开发一个涉及贷款的程序,并根据用户输入的内容向用户提供有关贷款的信息。

我编写这个程序的目的是让用户输入贷款金额和他们必须偿还的年数。一旦用户提供了这些信息,该程序就会获取贷款金额和年数,并告诉用户年利率、月供和总金额。此外,如果用户输入的贷款金额为-1,则程序应该终止。

以下是我迄今为止的代码:

package Loans;
import java.util.Scanner;
public class Loans {
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        double monthlyInterestRate;
        double annualInterestRate;
        double monthlyPayment;
        double total;
        double numberOfYears;
        double loanAmount;
        System.out.println("This program will compute the monthly payments and total payments for a loan amount on interest rates starting at 5%, incrementing by 1/8th percent up to 8%.");
        //Formula to Calculate Monthly Interest Rate:
        monthlyInterestRate = (annualInterestRate/1200);
        //Formula to Calculate Monthly Payment:
        monthlyPayment = (loanAmount*monthlyInterestRate);
        //Formula To Calculate Annual Interest Rate:
        annualInterestRate = (1-(Math.pow(1/(1 + monthlyInterestRate), numberOfYears * 12)));
        //Formula To Calculate The Total Payment:
        total = (monthlyPayment*numberOfYears*12);
        while(true)
        {

            System.out.println("Please enter in the loan amount.");
            double loanAmount = input.nextDouble();
            System.out.println("Please enter in the number of years.");
            double numberOfYears = input.nextDouble();
            System.out.println("Interest Rate: " + annualInterestRate);
            System.out.println("Monthly Payment: " + monthlyPayment);
            System.out.println("Total Payment: " + total);
            }
    }
}

这不会编译,我不知道为什么。(再说一遍,我是个初学者)

我收到的错误出现在"double loanAmount=input.nextDouble();"one_answers"double numberOfYears=input.next double(;)"的行上。

第一行的错误是"重复的本地变量loanAmount"。

第二行的错误是:"这一行有多个标记-行断点:贷款[行:39]-main(String[])-重复本地变量numberOfYears"

如有任何反馈,我们将不胜感激。

您得到的错误几乎是不言自明的。您对"贷款金额"one_answers"年数"的定义主要有两次。要么重命名它们,要么只声明一次。

如果您是编码初学者,我建议您使用Eclipse或Netbeans这样的IDE。他们将指出编译错误,并就如何修复这些错误提出建议。

很容易修复。因此,问题是在这个领域:

        System.out.println("Please enter in the loan amount.");
        double loanAmount = input.nextDouble();
        System.out.println("Please enter in the number of years.");
        double numberOfYears = input.nextDouble();

您正在重新定义双变量loanAmount和numberOfYears,这将导致错误。去掉两行中使用的"double"。

您需要做的另一个更改是在代码顶部初始化这些变量。更改初始化所有双变量的行,并将它们设置为0,例如:

    double annualInterestRate = 0;
    double numberOfYears = 0;
    double loanAmount = 0;

当变量在程序中有调用时,必须首先对其进行初始化。它被初始化为什么并不重要,因为它的最终值将由程序的操作决定,但它绝对需要在某个时刻被初始化。

我确实做了这些更改,程序编译成功。

希望这能有所帮助!

相关内容

最新更新