q- package character;
- import java.util.Scanner;
- public class CheckQQ {
- /**
- * 需求:模拟检查一个QQ号
- * 1 号码为 5-15为数字
- * 2 号码不能以0 开头
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(checkQQ());
- }
- public static boolean checkQQ(){
- //获取qq号
- String qqNum = new Scanner(System.in).nextLine();
- //字符串长度
- int length = qqNum.length();
- if(qqNum.startsWith("0")){
- System.out.println("输入错误,不能以0开头!");
- return false;
- }
- if(length>=5 && length<=15){
- char[] arr = qqNum.toCharArray();
- out: for (int i = 0; i < arr.length; i++) {
- char ch = arr[i];
- if(!Character.isDigit(ch)){
- System.out.println("输入非法字符!!");
- break out;
- }
- }
- System.out.println("输入字符正确!");
- }
- else{
- System.out.println("输入字符长度错误!");
- return false;
- }
- System.out.println("你的输入为:"+qqNum);
- return true;
- }
- }
复制代码
|
|