第一题:实现用户注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同。
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String name = sc.next(); if (name.length()>=3){ System.out.println("请输入密码:"); String password = sc.next(); if (password.length() >= 6){ System.out.println("请再次输入密码:"); String password2 = sc.next(); if (password2.equals(password)){ System.out.println("恭喜你,注册成功!"); }else { System.out.println("两次密码不相同,注册失败"); } }else { System.out.println("密码长度小于6位"); } }else { System.out.println("用户名长度小于3位"); } }
第二题:输入一个字符串(不含空格),输入一个字符,判断该字符在该字符串中出现的次数
public static void main(String[] args) { new Demo().Sub(); } public void Sub(){ int times=0; String zfc=new Scanner(System.in).next(); String zf=new Scanner(System.in).next(); for(int i=0;i第三题:由系统生成一个4位的纯数字的验证码字符串,提示用户输入验证码,判断验证码是否正确
public static void main(String[] args) { String str = "1234567890"; StringBuffer sb = new StringBuffer(4); for (int i = 0; i < 4; i++) { char ch = str.charAt(new Random().nextInt(str.length())); sb.append(ch); } String s = sb.toString(); System.out.println(s); System.out.println("请输入上面验证码:"); Scanner sc = new Scanner(System.in); String i = sc.next(); if (s.equals(i)){ System.out.println("验证码输入正确"); }else { System.out.println("验证码输入错误"); } }第四题: String str = "asdfhjka12347897KGKJHJKH/..’;[][";
统计以上字符串当中大写字母、小写字母、数字、符号出现的次数
public static void main(String[] args) { String str = "asdfhjka12347897KGKJHJKH/..’;[]["; int count = 0; int bigCount = 0; int smallCount = 0; int other = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c>='A'&& c <= 'Z'){ bigCount++; }else if (c>='a'&&c<='z'){ smallCount++; }else if (c>='0'&&c<='9'){ count++; }else { other++; } } System.out.println("大写字母出现的次数:"+bigCount+"小写字母出现的次数:"+smallCount+"数字出现的次数:"+count+"符号出现的次数:"+other); }以上就是本次的练习,下次见。