要求:
-
输入字符串表示手机号码,字符串长度必须等于11位,如果长度不等于11位,输出“手机号码位数错误”
-
输入字符串只能包含数字,不能包含其他字符,如果包含其他字符,输出"请输入数字"
-
如果输入错误,自动开始重新输入
-
如果输入正确,输出"输入正确",程序结束
public class Zifuchuan {
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true) {
System.out.println("请输入手机号:");
Scanner scan = new Scanner(System.in);
String tel = scan.next();
byte[] a = tel.getBytes();
if(tel.matches("-?\d+")) {
if (a.length == 11) {
System.out.println("输入正确");
break;
} else {
System.out.println("手机号码位数错误");
}
}
else {
System.out.println("请输入数字");
}
}
}
}