<identifier> expected&amp; .class预期java

时间:2015-10-26 08:13:37

标签: java compilation compiler-errors identifier

我对java有点新,所以请原谅我,如果我犯了一个简单的错误,但我不确定我哪里出错,我收到的错误是“.class预期,标识符预期,不是声明, ';'预期。”我尝试了不同的方式,并从这些方式收到不同的错误,但我认为这些错误将是最容易诊断的。提前感谢您的回复。     守则:

//main class 
Operation a = new Operation(); 
double area = a.square(double area, double length); // line that receives errors

// Operation class
import java.util.Scanner;
public class Operation
{
    double pi = 3.14;
    double gravity = 9.8;
    double length;
    double width;
    double area;
    Scanner keyboard = new Scanner(System.in);
    public double square(double area, double length)
    {
        System.out.println("Enter length of each side of a square:");
        length = keyboard.nextDouble();
        area = length * length;
        System.out.println(area);
        return area;
    }
}

2 个答案:

答案 0 :(得分:1)

您正在从命令行读取输入,因此您不需要square(...)方法中的任何参数。声明为:

public double square()
{
    double area, length;
    System.out.println("Enter length of each side of a square:");
    length = keyboard.nextDouble();
    area = length * length;
    System.out.println(area);
    return area;
}

并在主方法中调用它:

double area = a.square();

答案 1 :(得分:1)

在Java中定义方法时,必须像public double square(double area, double length)一样指定类型。

但是,在调用方法时,必须省略类型。您只传递变量:

double area = a.square(area, length);

Parker_Halo在答案中指出,你根本不需要这些参数。