《JAVA 学习 》String 字符串函数运用练习题

 Homework02

package com.hspedu.Exercise0;

import java.util.Scanner;

/**
 * @author Lei jinquan  ~
 * @version 1.0
 */
public class Homework02 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String user = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        System.out.println("请输入邮箱");
        String email = scanner.next();
        try {
            check(user, password, email);
            System.out.println("恭喜你注册成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void check(String user, String password, String email) {

        if (!(user.length() <= 4 && user.length() >= 2)) { //第一关
            throw new RuntimeException("用户名的字符溢出!");
        }
        if (!(password.length() == 6 && isDigital(password))) {//第二关
            throw new RuntimeException("您的密码长度为6,且为数字!");
        }
//        if (!(password.length() == 6)) {
//            throw new RuntimeException("密码不符合长度为6");
//        }
//        try {
//            Integer.parseInt(password);
//        } catch (NumberFormatException e) {
//            System.out.println("注册失败输入的密码不是纯数字" + e.getMessage());
//            return;
//        }
        int temp = email.indexOf("@");
        int temp1 = email.indexOf(".");
        if (!(temp > 0 && temp1 > temp)) {
            throw new RuntimeException("邮箱格式未包含@.且@在.后");
        }

    }

    public static boolean isDigital(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] < '0' || chars[i] > '9') {//Asics码值比较
                return false;
            }
        }
        return true;
    }


}

 Homework03        运用sqlit和format进行重新排序

package com.hspedu.Exercise0;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.jar.JarEntry;

/**
 * @author Lei jinquan  ~
 * @version 1.0
 */
public class Homework3 {
    public static void main(String[] args) {


        changename("Han shun Ping");

    }


    public static void changename(String str) {
        if (str == null) {
            System.out.println("输入值为空");
            return;
        }
        String[] names = str.split(" ");//分割成数组
        if (names.length != 3) {
            System.out.println("输入的字符串格式不对");
            return;
        }
        String format = String.format("%s,%s .%c", names[2], names[0], names[1].toUpperCase().charAt(0));
        //toUpperCase().charAt(0)首字母大写

        System.out.println(format);

    }

}

判断字母大小写和其他

 

package com.hspedu.Exercise0;

import java.util.zip.CheckedOutputStream;

/**
 * @author Lei jinquan  ~
 * @version 1.0
 */
public class Homework04 {

    public static void main(String[] args) {

        String str = "Lei Jin Quan";
        big(str);
    }

//    public static void big(String str) {
//        if (str == null){
//            System.out.println("输入值为空");
//            return;
//        }
//        int count = 0;
//        char[] chars = str.toCharArray();
//        for (int i = 0; i <chars.length ; i++) {
//            if ( chars[i] >= 'A' && chars[i] <= 'Z'){
//                count ++ ;
//            }
//        }
//        System.out.println("大写字母有" + count + "个");
//    }
//

    public static void big(String str) {
        int Strlen = str.length();
        int SpaceNum = 0;
        int lowerCount = 0;
        int bigCount = 0;
        int  otherCount = 0;
        for (int i = 0; i <Strlen ; i++) {
            if (str.charAt(i) == ' '){
                SpaceNum++;
            }else if (str.charAt(i)>= 'a'&& str.charAt(i)<='z'){
                lowerCount++;
            }else if (str.charAt(i)>= 'A'&& str.charAt(i)<='Z'){
                bigCount++;
            }else{
                otherCount++;
            }
        }

        System.out.println(" 空格数量为 " + SpaceNum + " 小写字母数量为 " + lowerCount + " 大写字母数量为 " + bigCount
                + " 其他字母数量为 "+ otherCount);

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值