我正在尝试制作一个程序来计算字符串的单词。
以下是我的代码,以及我的代码中出现的错误,我无法更正它们:
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.