用java语言编写输入一些数字,以","间隔,并且用回车结束,最后统计输入了多少个数字?

来源:百度知道 编辑:UC知道 时间:2024/06/12 04:44:59
import java.util.*;
public class Test {
public static void main(String[]args){
Scanner in=new Scanner(System.in);
String temp=in.nextLine();
int count=0;
for(int i=0;i<temp.length();i++){
if(Character.isDigit(temp.charAt(i)))
count++;
}
System.out.println("The amount of number is "+count);
}
}
现在要求输入12,23,567,输出:总共有3个数字
要加个判断为数字语句.

String 没有直接判断一个字符串是否是数字的方法,
前两天写了一个跟你需求基本吻合的问题,你可以看看

http://zhidao.baidu.com/question/39418905.html

好像split之后的数组的长度比你期望的值要多一,要仔细check一下

不知数字是否是严格限制啊,整数or实数?
楼上用个正则表达式验证是个好方法