java字符串练习

发布于:2022-12-22 ⋅ 阅读:(277) ⋅ 点赞:(0)
package study.StringName;

import java.util.Scanner;

public class Login {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //以知正确的用户名和密码,请用程序实现模拟用户登录总共给三次机会登录后给出提示。

        String name = "wangjie";
        String password = "123456";
        for (int i = 0; i < 3; i++) {

            System.out.println("请输入用户名:");
            String na = sc.next();
            System.out.println("请输入用户名密码");
            String pa = sc.next();

            if (na.equals(name) && pa.equals(password)){
                System.out.println("登录成功");
                return;
            }else {
                if (i==2){
                    System.out.println("账号被锁定");
                }else {
                    System.out.println("登录失败,用户名或者密码错误,你还剩下"+(2-i) +"次机会");
                }

            }

        }



    }

}

package study.StringName;

import java.util.Scanner;

public class Str02 {
    public static void main(String[] args) {
        //键盘录入字符串,统计该字符中大写小写字母和数字字符出现的次数(不考虑其他字符)

        //1.键盘录入字符串
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入字符串");
        String s = scanner.next();

        //2.统计---计数器思想
        int bigCount = 0,smallCount = 0,numCount = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c >= 'a'&& c <= 'z'){
                smallCount++;
            }else if (c >= 'A'&& c <= 'Z'){
                bigCount++;
            }else {
                numCount++;
            }
        }
        System.out.println("大写字母:"+bigCount+"小写字母:"+smallCount+"数字:"+numCount);
    }
}

 

package study.StringName;

public class Str03 {
    public static void main(String[] args) {
        int[] arr = {1,2,3};
        String s = arrToString(arr);
        System.out.println(s);
    }



    public static String arrToString(int[] arr){
        if (arr==null){
            return "";
        }else if (arr.length == 0){
            return "[]";
        }

        String re = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i== arr.length-1){
                re = re + arr[i];
                break;
            }
             re = re + arr[i]+",";
        }
        re = re + "]";
        return re;
    }
}

 

package study.StringName;

import java.util.Scanner;

public class Str04 {
    public static void main(String[] args) {
        //字符串反转
        //1.键盘录入abc,然后打印cba
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        Print(s);

    }


    public static void Print(String s){
        String str = "";
        for (int i =  s.length()-1; i >=0; i--) {
            char a = s.charAt(i);
            str = str + a;
        }
        System.out.println(str);
    }

}

 

 

package study.StringName;

import study.Scanner.Sca;

import java.util.Scanner;

public class Str05 {
    public static void main(String[] args) {

        int money =  0;

        while (true) {
            //1.键盘录入一个金额
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个金额");
            money = sc.nextInt();
            if (money >= 0 && money <= 999999){
                break;
            }else {
                System.out.println("金额无效");
            }
        }

        String strRuselt = "";
        while (true){
            int shu = money%10;
            String capitalNumber = getCapitalNumber(shu);

            strRuselt = capitalNumber + strRuselt;
            money /= 10;

            if (money==0){
                break;
            }
        }

        int count = 7 - strRuselt.length();

        for (int i = 0; i < count; i++) {
            strRuselt  = "零" + strRuselt;
        }

        System.out.println(strRuselt.charAt(0)+"佰"+strRuselt.charAt(1)+"拾"+strRuselt.charAt(2)
                +"万"+strRuselt.charAt(3)+"仟"+strRuselt.charAt(4)+"佰"+strRuselt.charAt(5)+"拾"+strRuselt.charAt(6)+"元");

        String[] arr = {"佰","拾","万","仟","佰","拾","元"};
        String mon = "";
        for (int i = 0; i < strRuselt.length(); i++) {
            mon = mon + strRuselt.charAt(i) + arr[i] ;
        }
        System.out.println(mon);


    }



    //定义一个方法把数字变成大写的中文
    public static String getCapitalNumber(int number){
        String[] str = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        //返回结果
        return str[number];
    }

}

 

 

package study.StringName;

public class Str06 {
    public static void main(String[] args) {
        //获取手机号
        String phoneNumber = "15912345678";

        //所取范围内截取
        String substring = phoneNumber.substring(0, 3);

        //截取7索引后面的数字
        String substring1 = phoneNumber.substring(7);

        String s = substring +"****"+ substring1;

        System.out.println(s);

    }
}

 

 

package study.StringName;

public class Str07 {
    public static void main(String[] args) {

        //1.定义一个身份证号码
        String id = "321281202001011234";

        //2.获取出生年月日
        String year = id.substring(6, 9);
        String month = id.substring(10, 12);
        String day = id.substring(12, 14);

        System.out.println("出生年月:"+year+"年"+month+"月"+day+"日");

        //3.获取性别
        char gender = id.charAt(16);
        int xb = gender - 48;

        if (xb%2==0){
            System.out.println("性别为:女");
        }else {
            System.out.println("性别为:男");
        }
        //4.最后打印出来
    }


}

 

 

package study.StringName;

public class Str08 {
    public static void main(String[] args) {
        String talk = "你玩的真好,以后不要在玩了,TMD";

        String[] arr = {"TMD","CNM","SB","mlgb"};

        //循环查询敏感词然后替换
        for (int i = 0; i < arr.length; i++) {
             talk = talk.replace(arr[i], "***");
        }
        System.out.println(talk);


    }
}

本文含有隐藏内容,请 开通VIP 后查看