栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Java API之String练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java API之String练习题

第一题:实现用户注册,要求用户名长度不小于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);
    }

 以上就是本次的练习,下次见。

 

 

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039552.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号