从键盘录入一行字符串,里面有字母、整数和小数形式(如:“abc-12-ddd-15.6-28-14.4-kuf”),请计算字符串中所有整数形式字符串对应整数之和、所有小数形式字符串对应小数的平均值(采用函数定义和调用的方式)。
import java.util.Scanner;
public class Test {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("输入字符串:");
String strs = input.next();
int number = 0;
int pj=0;
int chara = 0;
int other = 0;
char [] chs = strs.toCharArray();
for(char c : chs){
if(c >= '0' && c <= '9'){
pj+=Integer.valueOf(c);
number++;
}else if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ){
chara++;
}else{
other++;
}
}
System.out.println("数字有:" + number + "个,"平均值:" + (pj/number) + ",字符有" + chara + "个,其他有:" + other + "个。" );
}
}