java java中的“.class预期错误”是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26437719/
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
What does" .class expected error " mean in java
提问by agana sathya
Here is my code. I got an error as ".class expected". What should I do to rectify it.
这是我的代码。我收到了“.class 预期”的错误。我该怎么做才能纠正它。
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
double cur = acnt_balc - (withdraw + 0.50);
System.out.println(cur);
else
System.out.println(acnt_balc);
}
}
回答by Patricia Heimfarth
Try curly braces in the context of your if-else. It should look like this:
在 if-else 的上下文中尝试使用花括号。它应该是这样的:
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
So you can see, that there is a if-block and a else-block. You have to say which code belongs to if and which belongs to else. You can do that with the braces.
所以你可以看到,有一个 if 块和一个 else 块。您必须说明哪些代码属于 if 哪些属于 else。你可以用大括号做到这一点。
回答by Sainath S.R
Scanner requires you to import java.util.*;
Scanner 需要你导入 java.util.*;
I tried it works and also format properly , see below , it runs
我试过它可以工作并且格式正确,见下文,它运行
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >=(withdraw + 0.50)))
{
double cur=(acnt_balc-(withdraw + 0.50));
System.out.println(cur);
}
else
{
System.out.println(acnt_balc);
}
}
}
Also make sure your Filename is the same as class name , in this case Atm2.java
还要确保您的文件名与类名相同,在本例中为 Atm2.java
The Scanner Class in present in the 'java.util' package , since you forgot to import it and used Scanner sc=new Scanner(); the compliler is complaining, and remember to compile use javac filename.java , to run use java filename
'java.util' 包中存在 Scanner 类,因为您忘记导入它并使用了 Scanner sc=new Scanner(); 编译器在抱怨,记得编译 use javac filename.java ,运行 use java filename