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

标签 java program-entry-point

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

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

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();
}

错误:

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.

最佳答案

你的主课不在了。在类中声明它。

关于java:25: 类、接口(interface)或枚举预期 public static void main(String s[]) {,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584323/

相关文章:

c - 编译器如何知道我的 main 函数在哪里?

java - 没有 LinkedList 的替代方法吗?

java - 数组索引越界异常: 12

java - 我需要一个 JTextArea,它在行的末尾计算该行中的字符数

c# - 在 WPF 的自定义 main() 中获取对 MainWindow 的引用

python - 如何向一个输入变量添加不同的金额?

java - 在 list 文件中设置主类的路径

java - 当tomcat启动时,它使用错误的JAVA版本是1.5而不是1.6

java - 该方法属于Value对象还是Manager

使用 IntelliJ 调试 Go 单个文件