【发布时间】:2018-11-28 00:48:01
【问题描述】:
我想知道为什么计数除了 0 之外什么都不返回。我想知道是否可以在没有数组的情况下以这种方式完成。感谢您的帮助,谢谢!
String word= "";
int value = 0;
while(!word.equalsIgnoreCase("Exit")){
System.out.print("Type words or exit to quit: ");
word = scan.nextLine();
}
value = numberCount(word);
System.out.print("The number of words that start with a number is "+value);
}
public static int numberCount(String str){
int count =0;
char c = str.charAt(0);
if(c >= '0' && c <= '9'){
count++;
}
return count;
}
}
【问题讨论】:
-
您是否尝试过调试以查看 numberCount 中的 if 条件会发生什么?
-
word是"Exit"而你只是循环直到它是。怎么以数字开头? -
考虑删除您的
numberCount()方法并替换为if (word.matches("\\d.*")) count++;
标签: java count numbers word charat