如果尾数为8,需支付办卡费50元
如果尾数为4,需支付办卡费用0元
如果是其他尾号,需支付办卡费用20元
package test;
import java.util.Scanner;
//如果尾数为8,需支付办卡费50元
//如果尾数为4,需支付办卡费用0元
//如果是其他尾号,需支付办卡费用20元
public class test11 {
public static void main(String[] args) {
//1.提示并接收用户输入的手机号
System.out.println("请输入您预选的手机号");
String tel = new Scanner(System.in).nextLine();
//注意String类型数据一般用next
//2.调用getPrice方法,计算价格,注意需要把手机号作为tel作为参数传入方法中
getPrice(tel);
}
//3.创建getPrice()
private static void getPrice(String tel) {
//3.1手机号tel的长度,必须是11位的正确手机号---提前校验,提高程序健壮性
if(tel.length()!= 11){
System.out.println("号码格式错误");
return;//结束方法,返回到调用者位置再继续执行
}
/*
比如我们拿到的手机号是tel="2313123123",想要拿到尾数,也就是最后一个字符
需要用到 char c = tel.charAt(10)
注意下标是从0开始,所以取第11个字符,它的下标位置是10
*/
//3.2获取手机号的尾数
char c = tel.charAt(10);
//定义用来保存最终结果的变量price,初始值为0
int price = 0;
//根据字符c的值,来为price重新赋值
switch(c){
case '8':price=50;break;//尾数为8,支付50
case '4':price=20;break;//尾数为4免费
default: price=20;//其他情况20
}
//显示结果
System.out.println("您应支付的价格为: "+price);
}
}