java - 我找不到错误代码为 "Cannot find symbol"的原因

标签 java

我试图弄清楚为什么当“monthlyPayment”位于我调用的方法的参数“interest_Total”内时找不到它(我在它旁边用两个星号标记它)。下面是我的代码,后面是错误。

import java.util.*;
public class TestCC
{
   public static void main(String[] args)
  {
  Scanner kb = new Scanner(System.in);
  
  System.out.println("Welcome to the payment calculator");
  System.out.println("List of options");
  System.out.println("MP: To calculate the Monthly Payment for a fix-rate, fix-term loan");
  System.out.println("M: To calculat ethe number of Months to pay off a loan witha  fixed monthly payment");
  
  System.out.print("Please enter MP or M: ");
  String choice = kb.next();
  
  while (!(choice.equals("MP") || choice.equals("M")))
  {
     System.out.print("Error; Enter MP or M: ");
  }
  
  if (choice.equals("MP")) 
  {
     // Loan Amount
     
     System.out.print("Enter loan amount: ");
     while (!kb.hasNextDouble())
     {
        kb.next();
        System.out.print("Enter loan amount: ");
     }
     double loan = 0;
     loan = kb.nextDouble();
     
     
     // Term Amount
     
     System.out.print("Enter term in years: ");
     while (!kb.hasNextInt())
     {
        kb.next();
        System.out.print("Enter term in years: ");
     }
     int years = 0;
     years = kb.nextInt();
     
     
     // Interest Rate
     
     System.out.print("Enter the interest rate: ");
     while (!kb.hasNextDouble())
     {
        kb.next();
        System.out.print("Enter the interest rate: ");
     }
     double interestRate;
     interestRate = kb.nextDouble(); 
              
     // Calling methods Part 1
     payment(loan, years, interestRate);
     
     **interest_Total(monthlyPayment, years, loan);**
       
  }
 }
public static double payment(double loan, int years, double interestRate)

  {
  double monthlyPayment = 0;
        
  monthlyPayment = (loan * (interestRate/12))/(1 - (1/(Math.pow((1 + (interestRate/12)),(years * 12)))));
  System.out.printf("Monthly Payment: $%.2f", monthlyPayment);
  
  return monthlyPayment;                                    
  }

public static double interest_Total(double monthlyPayment, int years, double loan)

  {
  double totalInterest2 = 0;
  totalInterest2 = ((monthlyPayment * years * 12) - loan);
  System.out.printf("Total Interest Paid: %.2f", totalInterest2);
  
  return totalInterest2;
 }
}

下面是我得到的错误

TestCC.java:63: error: cannot find symbol
     interest_Total(monthlyPayment, years, loan);
                    ^
 symbol:   variable monthlyPayment
 location: class TestCC
 1 error

最佳答案

monthlyPayment 是interest_Total 函数的参数名称。 MonthlyPayment 是付款函数中的变量。但是在您调用该函数的 main 中没有任何名称。也许这就是你的意思?

double monthlyPayment = payment(loan, years, interestRate);
interest_Total(monthlyPayment, years, loan);

关于java - 我找不到错误代码为 "Cannot find symbol"的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62482292/

相关文章:

java - 如何在自定义 Eclipse 插件中访问默认的 JDT 完成建议?

java - 可变数量的嵌套 For 循环

java - 如何使 JDialog 不总是在父级之上

java - 使用正则表达式获取重复模式

java - 根据测试配置,将 logback.xml 配置为将所有日志消息输出到一个文件或每个测试的单独文件中

java - 为什么带有前导零的整数文字会被奇怪地解释?

java - @Configuration 类的 bean 是否始终为 null?

java - 无法使用 JAAS 身份验证连接到 ActiveMQ

java - Selenium、Chrome 的 WebDriver 停止窗口打开

java - 仅在创建新文件时删除 .dat 文件