新建Test类,在Test类中定义两个方法f1和f2,其中
1、方法f1需要传入参数s(String类型)实现以下功能:判断传入的参数是否为中国电信的手机号码。中
国电信的手机号码段为:133 149 153 173 177 180 181 189 199。
2、方法f2实现以下功能:解析字符串:"数学87分,物理76分,英语96分"中的考试成绩,并计算出总成绩
以及平均分数。
public class Test {
public static void main(String[] args) {
System.out.println(f1("13312341234"));
f2("数学87分,物理76分,英语96分");
}
public static boolean f1(String s) {
return (s.startsWith("133") || s.startsWith("149")
|| s.startsWith("153") || s.startsWith("173") || s.startsWith("177")
|| s.startsWith("180") || s.startsWith("181") || s.startsWith("189") || s.startsWith("199"));
}
public static void f2(String s) {
String[] ss = s.split("[^0-9]");
int total = 0;
float average = 0f;
int num = 0;
for (String s1 : ss) {
if (s1 != null && !s1.isEmpty()) {
total += Integer.parseInt(s1);
num++;
}
}
average = total / (float) num;
System.out.println("total = " + total + " average = " + average);
}
}