简体   繁体   中英

Cannot find symbol in simple and slightly redundant code

when I compile this code it gives me the error: cannot find symbol > investmentAmount in the last segment. If I take out said segment:

JOptionPane.showMessageDialog(null, "The total amount you have to pay after 5 years is " 
+ Math.pow(investmentAmount(interestRate+1),5));

then the rest of the code works, except that I need that segment of the code to pass. If I take out investmentAmount in this segment, but keep interestRate, then the code works perfectly, and gives me the amount after five years. Why does investmentAmount works earlier in the code and not at the end? Why does interestRate not mess up the code at the end and investmentAmount does? Here's the whole code-

import javax.swing.JOptionPane;

public class Ch3IndLabAssignment
{
public static void main( String [] args )
{
String investorName = JOptionPane.showInputDialog( null, "Please enter your first and last name");
JOptionPane.showMessageDialog(null, "Your name is " + investorName);

String input = JOptionPane.showInputDialog( null, "Please enter your investment amount");
double investmentAmount = Double.parseDouble(input);
JOptionPane.showMessageDialog(null, "Your investment amount is " + investmentAmount);

String input2 = JOptionPane.showInputDialog( null, "Please enter the interest rate as a decimal");
double interestRate = Double.parseDouble(input2);
JOptionPane.showMessageDialog(null, "Your interest rate is " + interestRate);

JOptionPane.showMessageDialog(null, "The total amount you have to pay back after 5 years is " +
Math.pow(investmentAmount(interestRate+1),5));
}
}

In the code

JOptionPane.showMessageDialog(null, "The total amount you have to pay after 5 years is " 
+ Math.pow(investmentAmount(interestRate+1),5));

You treat investmentAmount as a method with one parameter, which is interestRate+1 . Perhaps you meant:

JOptionPane.showMessageDialog(null, "The total amount you have to pay after 5 years is " 
+ Math.pow(investmentAmount*(interestRate+1),5));

Notice the * .

You probably meant to write:

Math.pow(investmentAmount * (interestRate+1), 5));

In math, A(B) often means A is multiplied by B. In programming, it often means that A is a function, and B is one of its arguments. The compiler is looking for a method (not a variable) called investmentAmount - since it cannot find a method matching that symbol, you get your "cannot find symbol" error.

Most programming languages, including Java, don't perform implicit multiplication. You're trying to use your variable as a method name. Use * to multiply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM