在解析时到达文件末尾,,
我似乎无法让这个错误消失。它说在最后一行:解析时到达文件结尾,任何人都可以帮助我吗?在解析时到达文件末尾,,
class scanner
{
class Factorial
{
public static void main(String args[])
{
int n, c, fact;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1 ; c <= n ; c++)
fact = fact *c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
您最后缺少一个括号“}”,这就是为什么它会抛出错误。
更正后的代码语法,也解决了其他错误。
编辑:作为@Pshemo说,你并不需要的“类扫描仪”,让我评论说,但你可以删除的内容被注释掉真正
import java.util.Scanner;
//public static class scanner
//{
public static class Factorial
{
public static void main(String args[])
{
int n, c, fact;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1 ; c <= n ; c++)
fact = fact *c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
//}
UMN,好吧,我把它放在那里,但我现在得到另一个不同类型的错误,那就是:行:10 找不到符号 符号:类扫描仪 位置:类scanner.Factorial 线:10 找不到符号 符号:类扫描器 位置:类scanner.Factorial 线:在内部类scanner.Factorial 5 非法静态声明 修饰符“静态”在恒定变量声明只允许 – user3015774
读取错误。您需要导入“扫描仪”库。如果你想要一个“public static void main()”,你需要声明类为“public static”。我也更新了解决这些错误的答案。 –
没有像静态外部类(除非它也在其他类中),所以你应该在'scanner'类声明中跳过'static'。 – Pshemo
开始使用缩进。它会让你更容易发现错误。你也真的想要嵌套类(目前Factorial是在扫描仪内)。另一件事:不要用你已经使用过的名称调用你的类,就像你的情况一样,Java已经有'Scanner'类 - 你忘记了导入的'java.util'包 - 所以你的类不应该被称为'scanner '。 – Pshemo