java:25: 类、接口或枚举预期 public static void main(String s[]) {

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11584323/
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-10-31 05:40:38  来源:igfitidea点击:

java:25: class, interface, or enum expected public static void main(String s[]) {

javamain

提问by Nidhi Chhabra

I am trying to make a program to count words of a string.

我正在尝试制作一个程序来计算字符串的单词数。

The following is my code, and the errors coming in my code and I am unable to correct them:

以下是我的代码,以及我的代码中出现的错误,我无法纠正它们:

import java.util.*;    
class string1 {
    public static int wordcount() {
        String str;
        Scanner s1= new Scanner(System.in);
        System.out.println("Enter String:");
        str=s1.nextLine();
        int count=WCount(str);
        System.out.println("Count="+count);
    }

    public static int WCount(String str) {
        int l=str.length();
        int count=0;
        for(int i=0;i<l;i++) {
            if(str.charAt(i)==' ')
                count++ ;
        }
        if(count>0) 
            count++ ;
        return(count);
    }
}
public static void main (String s[]) {
    string1 ss=new string1();
    ss.wordcount();
}

Error :

错误 :

java:25: class, interface, or enum expected
public static void main(String s[]) {
          ^
C:\Users\coocl\Desktop\java\string1.java:27: class, interface, or enum expected
ss.wordcount();
^
C:\Users\coocl\Desktop\java\string1.java:28: class, interface, or enum expected}3 errors

Process completed.

回答by JProgrammer

You main is out of the class. Declare it inside the class.

你主要不在课堂上。在类中声明它。

回答by pb2q

The mainmethod belongs insideyour class. When you run javato execute your compiled code, it will try to run the mainmethod that belongs to the class that you specify. More generally, methods can'tbe declared outside classes in java: all methods must belong to a class.

main方法属于内部类。当您运行java以执行已编译的代码时,它将尝试运行main属于您指定的类的方法。更一般地说,方法不能在 java 中的类之外声明:所有方法都必须属于一个类。

Note that since your methods wordcountand WCountare both static, you don't need to create a string1instance to use them, you can just call them on the class in main:

请注意,由于您的方法wordcountWCount都是静态的,您不需要创建string1实例来使用它们,您只需在 中的类上调用它们main

string1.wordcount();

Finally, in java, it is conventional to begin class names with uppercase, e.g. String1, see these Naming Conventions.

最后,在 Java 中,类名通常以大写开头,例如String1,请参阅这些命名约定

回答by SLaks

You have an extra }after WCount.

}在 WCount 之后你有一个额外的。

回答by Luiggi Mendoza

The mainmethod must be inside a class, not outside. You have two ways:

main方法必须在类内部,而不是外部。你有两种方法:

  1. Move it inside your string1class.

  2. Move it inside a class in the same *.java file

    public class Main {
        public static void main (String s[]) {
            string1 ss=new string1();
            ss.wordcount();
        }
    }
    
  1. 把它移到你的string1班级里。

  2. 将它移动到同一个 *.java 文件中的类中

    public class Main {
        public static void main (String s[]) {
            string1 ss=new string1();
            ss.wordcount();
        }
    }
    

回答by Andrey.Pushin

Bad practice:

不好的做法:

string1 ss=new string1(); 
ss.wordcount(); 

First char in Java class name must be UPPER and
First char in Java method and fields name must be LOWER case;
All chars in JAVA constants (static final) name must be UPPER case;
wordCount() = static method and create object (string1 ss=new string1()) is not true.
Static fields and methods need call from class name (not instance class).
ClassName.(method/field)
Must be String1.wordCount()

Java 类名中的第一个字符必须是大写,
Java 方法和字段名中的第一个字符必须是小写;
JAVA 常量(静态最终)名称中的所有字符必须大写;
wordCount() = 静态方法和创建对象 (string1 ss=new string1()) 是不正确的。
静态字段和方法需要从类名(不是实例类)调用。
ClassName.(method/field)
必须是 String1.wordCount()

Your code have multiple problem (in "{}", "return" in wordcount()) See code:

您的代码有多个问题(在 wordcount() 中的“{}”、“return”中)请参阅代码:

public class Test {
    public static int wordcount() {
        String str;
        Scanner s1 = new Scanner(System.in);
        System.out.println("Enter String:");
        str = s1.nextLine();
        int count = WCount(str);
        System.out.println("Count=" + count);
        return count;
    }

    public static int WCount(String str) {
        int l = str.length();
        int count = 0;

        for (int i = 0; i < l; i++) {
            if (str.charAt(i) == ' ')
                count++;
        }
        if (count > 0)
            count++;
        return (count);
    }

    public static void main(String s[]) {
                Test.wordcount();
    }
}