【问题标题】:What causes error "error: '.class' expected"? [duplicate]是什么导致错误“错误:'.class'预期”? [复制]
【发布时间】:2021-12-19 06:32:42
【问题描述】:

请帮我解决这些错误。
这些是我在执行下面的 java 代码后遇到的错误。

/Deposit.java:15: error: '.class' expected
    double results=presentValue(double f, double r ,int n);
                                       ^
/Deposit.java:15: error: ';' expected
    double results=presentValue(double f, double r ,int n);
                                        ^
/Deposit.java:15: error: <identifier> expected
    double results=presentValue(double f, double r ,int n);
                                                    ^
/Deposit.java:15: error: ';' expected
    double results=presentValue(double f, double r ,int n);
                                                         ^
4 errors

import javax.swing.JOptionPane;

/*This program computes a customers present
deposit to make to obtain a desired future
value
*/
public class Deposit
{
  //Main method
  public static void main(String[] args)
  {

    //Calling the present value method
    double results=presentValue(double f, double r ,int n);

    //Displaying the present value 
    JOptionPane.showMessageDialog(null, "You have to deposit: $" +results);
  }

  public static double presentValue(double f, double r, int n)
  {
    //Declaring the input variable
    String input;

    //Taking inputs from the customer
    //Future value
    input = JOptionPane.showInputDialog("Enter your desired future value:");
    f = Double.parseDouble(input);

    //Annual Interest Rate
    input = JOptionPane.showInputDialog("Enter the annual interest rate:");
    r = Double.parseDouble(input);

    //Number of years
    input = JOptionPane.showInputDialog("Enter the number of years:");
    n = Integer.parseInt(input);

    //Calculating the present value the customer has to deposit
    double p = f/Math.pow((1+r), n);

    //Returning the value to the present value method
    return p;

    System.exit(0);
  }
}

【问题讨论】:

  • double results=presentValue(double f, double r ,int n);你没有在这里传递任何值,

标签: java swing compiler-errors joptionpane


【解决方案1】:

使用不带参数的函数,因为你的函数什么都不做。

public class Deposit
{
  //Main method
  public static void main(String[] args)
  {
    double results=presentValue();
    ... // same code
  }

  public static double presentValue()
  {
    double f,r;
    int n;
    ... // same code
  }
}

【讨论】:

    【解决方案2】:

    在调用方法时,切勿在方法的参数中提及数据类型。

    您的错误可以通过更改方法调用行来解决,

    确保您要么直接将值作为参数传递,要么在变量中声明这些值,如下所示。

    double f = 1.0d; // Just for Example
    double r = 2.0d; // Just for Example
    int n = 1; // Just assuming
    
    //Calling the method
    double results = presentValue(f, r ,n);
    

    【讨论】:

      猜你喜欢
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 2013-12-12
      • 1970-01-01
      相关资源
      最近更新 更多