大家帮我看看这个java小程序哪里出错了~~
publicclassCountWords{publicstaticvoidmain(Stringargs[]){charwords[]="Javaisanobjecto...
public class CountWords{
public static void main(String args[]){
char words[] = "Java is an object oriented programming language";
int counterWords = 0, counterNull = 0;
for( int i=0; i< words.length()-1; i++)
{
if( words[i].equals(' '))
{ counterNull++; }
else { counterWords++; }
}
System.out.println("words is " + counterWords);
System.out.println("AvgWords is " +(double)counterWords/counterNull);
}
}
用记事本写的,将就将就下哈
哎,学了C++,现在学Java,有些不习惯 展开
public static void main(String args[]){
char words[] = "Java is an object oriented programming language";
int counterWords = 0, counterNull = 0;
for( int i=0; i< words.length()-1; i++)
{
if( words[i].equals(' '))
{ counterNull++; }
else { counterWords++; }
}
System.out.println("words is " + counterWords);
System.out.println("AvgWords is " +(double)counterWords/counterNull);
}
}
用记事本写的,将就将就下哈
哎,学了C++,现在学Java,有些不习惯 展开
2个回答
展开全部
public static void main(String args[]) {
char[] words = "Java is an object oriented programming language".toCharArray();
int counterWords = 0, counterNull = 0;
for (int i = 0; i < words.length - 1; i++) {
if (words[i] == ' ') {
counterNull++;
} else {
counterWords++;
}
}
System.out.println("words is " + counterWords);
System.out
.println("AvgWords is " + (double) counterWords / counterNull);
}
错误除了楼上朋友说的以外还有几个地方
1.length是属性,不是方法,所以不需要length()
2.char 是原始类型,没有equals方法,所以只需要用==做比较就好,比较' '中间要一个空格,才可以,不能直接''。
char[] words = "Java is an object oriented programming language".toCharArray();
int counterWords = 0, counterNull = 0;
for (int i = 0; i < words.length - 1; i++) {
if (words[i] == ' ') {
counterNull++;
} else {
counterWords++;
}
}
System.out.println("words is " + counterWords);
System.out
.println("AvgWords is " + (double) counterWords / counterNull);
}
错误除了楼上朋友说的以外还有几个地方
1.length是属性,不是方法,所以不需要length()
2.char 是原始类型,没有equals方法,所以只需要用==做比较就好,比较' '中间要一个空格,才可以,不能直接''。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询