Java “错误:'.class' 预期”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19384681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:40:30  来源:igfitidea点击:

"Error: '.class' expected"

java

提问by Joseph Shepherd

So I'm new to Java, and my most recent piece of code has not worked. I get error: not a statementtwice and error: ';' expectedonce.

所以我是 Java 新手,我最近的一段代码没有工作。我得到error: not a statement两次和error: ';' expected一次。

If I try radius=double;, it appears with the error: Error: '.class' expectedon line 8, where the caret shows under the semi-colon.

如果我尝试radius=double;,它会出现错误:Error: '.class' expected在第 8 行,其中插入符号显示在分号下方。

I am not sure what is wrong, but here is my code. It isn't long... thank-you in advance.

我不确定出了什么问题,但这是我的代码。时间不长......提前谢谢你。

import java.util.Scanner;
import static java.lang.Math;

public class Formula {
    public static void main(String[] args);{
    double = radius;

        Scanner in = new Scanner(System.in);
        System.out.print("Radius of circle (cm) :> ");
        double radius = in.nextDouble();
        System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
        System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
        System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
        System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));

    }
}

采纳答案by Konstantin Yovkov

Change

改变

double = radius;

to

double radius = 0;

and remove the ;after the public static void main(String[] args);method definition.

并删除方法定义;之后的public static void main(String[] args);

Also, in the statement double radius = in.nextDouble();you will have to remove the doublekeyword, since you have already defined a variable with the same name.

此外,在语句中,double radius = in.nextDouble();您必须删除double关键字,因为您已经定义了一个具有相同名称的变量。

回答by RamonBoza

Remove the ';' from your main

去除那个 ';' 从你的主要

public static void main(String[] args);

to

public static void main(String[] args)

回答by Tim S.

Your variable declarations are confused: double = radius;doesn't make sense because doubleis a type, not a variable (and variable declarations look like type identifier = value;not type = identifier;or identifier = type;), and you declare but never use the radiumvariable. Should be something like:

你的变量声明很混乱:double = radius;没有意义,因为double是类型,而不是变量(变量声明看起来像type identifier = value;nottype = identifier;identifier = type;),你声明但从不使用radium变量。应该是这样的:

Scanner in = new Scanner(System.in);
System.out.print("Radius of circle (cm) :> ");
double radius = in.nextDouble();
System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));

And you shouldn't have that ;at the end of your mainmethod declaration line.

而且您不应该;main方法声明行的末尾使用它。

回答by Troubleshoot

You have three problems with your code:

您的代码存在三个问题:

Remove the semi-colon on the declaration of the main method.

删除 main 方法声明上的分号。

public static void main(String[] args)

Your second problem is that you do not have a reference for your double variable. All variables must have references. Consider your code which should be:

您的第二个问题是您没有双变量的引用。所有变量都必须有引用。考虑你的代码应该是:

double radius = 0;

doubleis the data type and radiusis the reference. In other words, double is the type of variable and radius is the name of the variable.

double是数据类型,radius是引用。换句话说,double 是变量的类型,radius 是变量的名称。

Your third problem is this line.

你的第三个问题是这条线。

double radium = in.nextDouble();

You should change it to:

您应该将其更改为:

radius = in.nextDouble();

All variables must be correctly referenced. Also by declaring the variable again, you're shadowing the old one.

必须正确引用所有变量。同样通过再次声明变量,您正在掩盖旧变量。



It would be better to instead of initializing the variable then initializing it again, delete your line:

最好不要初始化变量然后再次初始化它,删除您的行:

double = radius

or if you changed it to what I said above, remove

或者如果您将其更改为我上面所说的,请删除

double radius = 0;

回答by Alex Goja

culprit (i.e. wrong way of declaring a variable):

罪魁祸首(即声明变量的错误方式):

double = radius;

双 = 半径;

should be something of this form:

应该是这种形式的东西:

double radius = 0;

双半径 = 0;

However, I see you are using radius again, but this time correctly, to get the value entered by the user from the console, so you can safely delete the culprit statement and it should work.

但是,我看到您再次使用半径,但这次正确,从控制台获取用户输入的值,因此您可以安全地删除罪魁祸首语句,它应该可以工作。

回答by Aaron

The code that you have :

您拥有的代码:

import java.util.Scanner;
import static java.lang.Math;

public class Formula
{
public static void main(String[] args);
{
double = radius;

    Scanner in = new Scanner(System.in);
    System.out.print("Radius of circle (cm) :> ");
    double radius = in.nextDouble();
    System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
    System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
    System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
    System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));

    }
}

What it should look like :

它应该是什么样子:

import java.util.Scanner;
import static java.lang.Math;

public class Formula
{
public static void main(String[] args) {

double radius = 0;

    Scanner in = new Scanner(System.in);
    System.out.print("Radius of circle (cm) :> ");
    radius = in.nextDouble();
    System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
    System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
    System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
    System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));

    }
}

You had a semicolon after your method, declaring a double needs to equal something, and you don't need to say double radius = in.nextDouble();you just need radius = in.nextDouble();

你的方法后面有一个分号,声明一个 double 需要等于某物,你不需要说double radius = in.nextDouble(); 你只需要radius = in.nextDouble();