用户登陆

https://www.bilibili.com/video/BV17F411T7Ao/?p=100&spm_id_from=333.851.header_right.history_list.click&vd_source=94fb2114c3889338d33bcb41c3e25538

跟黑马讲的方法不太一样,这里简单用了一下创建对象,好处在于可以创建数组储存多个用户名和密码组合

package Land;

public class Land {
    private String username;
    private String passwd;


    public Land() {
    }

    public Land(String username, String passwd) {
        this.username = username;
        this.passwd = passwd;
    }

    /**
     * 获取
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置
     * @param username
     * @return
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取
     * @return passwd
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     * 设置
     * @param passwd
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
}
package Land;

import java.util.Scanner;

public class LandTest {
    public static void main(String[] args) {
        Land trueLands = new Land();
        trueLands.setUsername("admin");
        trueLands.setPasswd("admin");
        String trueUsername = trueLands.getUsername();
        String truePasswd = trueLands.getPasswd();

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            Land lands = new Land();
            System.out.println("请输入用户名:");
            String username = sc.next();
            lands.setUsername(username);
            System.out.println("请输入用密码:");
            String passwd = sc.next();
            lands.setPasswd(passwd);
			//判断用户名和密码是否正确
            if (username.equals(trueUsername) && passwd.equals(truePasswd)) {
                System.out.println("登陆成功");
                break;
            } else {
                if (i == 2) {
                    System.out.println("登陆失败, 您的登陆机会已经用完");
                } else {
                    System.out.println("登陆失败,用户名或密码错误,您还剩" + (2 - i) + "次机会");
                }
            }

        }

    }
}

统计字符次数

package test5;

import java.util.Scanner;

public class StringTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.next();
        int numberCount = 0;
        int bigCount = 0;
        int smallCount = 0;

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 'a' && c <= 'z') {
                smallCount++;
            } else if (c >= 'A' && c <= 'Z') {
                bigCount++;
            } else if (c >= '0' && c <= '9') {
                numberCount++;
            }
        }
        System.out.println("大写字母有:" + bigCount + "个");
        System.out.println("小写字母有:" + smallCount + "个");
        System.out.println("数字有:" + numberCount + "个");


    }
}

拼接及反转字符串

这两道练习题我就直接按一道题来写了,方便一点

package test5;

import java.util.Scanner;

public class StringTest1 {
    public static void main(String[] args) {
        System.out.println("请输入一个长度为5的字符串:");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String[] arr = getArr(str);
        System.out.println(spliceString(arr));
        System.out.println(reverseString("字符串反转后: "+str));
    }

    public static String spliceString(String[] arr) {
        if (arr == null) {
            return "";
        }
        if (arr.length == 0) {
            return "[]";
        }
        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            String c = arr[i];
            if (i == 4) {
                result = result + c;
            } else {
                result = result + c + ", ";
            }
        }
        result = result + "]";
        return result;
    }
    //反转字符串
    public static String  reverseString(String str) {
        String result = "";
        for (int i = str.length() - 1; i >=0; i--) {
            char c = str.charAt(i);
            result = result + c;
        }
        return result;
    }
    //将输入的字符串转化为数组
    public static String[] getArr(String str) {
        String[] arr = new String[5];
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            arr[i] = String.valueOf(c);//这是一个方法,将char类型转换为String类型,网上搜来的,具体可以看下图
        }
        return arr;
    }
}

转换金额

题目描述:键盘录入一个小于等于7位的数字,将其转换为如图形式

package test5;

import java.util.Scanner;

public class Money {
    public static void main(String[] args) {
        System.out.println("请输入money: ");
        Scanner sc = new Scanner(System.in);
        int money;
        //判断输入的数字是否有效
        while (true) {
            money = sc.nextInt();
            if (money > 0 && money <= 9999999) {
                break;
            } else {
                System.out.println("输入的数字无效,请重新输入");
            }
        }
        String result = "";
        //循环获取每个位置上的数字
        while (true) {
            int ge = money % 10;
            //调用写的方法将数字转换为大写汉语数字
            String bigNumber = getBigNumber(ge);
            //将结果反向加在一起
            result = bigNumber + result;
            money = money / 10;
            //如果money==0,则循环结束
            if (money == 0) {
                break;
            }
        }
        //补零
        result = addZero(result);
        //加单位
        result = addCompany(result);

        System.out.println("转换后金额为:\n"+result);
    }
    //将数字转换为大写数字
    public static String getBigNumber(int number) {
        String[] str = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        return str[number];
    }
    //在生成的大写数字前面补零
    public static String addZero(String str) {
        int count = 7 - str.length();
        for (int i = 0; i < count; i++) {
            str = "零" + str;
        }
        return str;
    }
    //补单位
    public static String addCompany(String arr) {
        String result = "";
        String[] str = {"佰", "拾", "万", "仟", "佰", "拾", "元"};
        for (int i = 0; i < arr.length(); i++) {
            char c = arr.charAt(i);
            result = result + c+str[i];
        }
        return result;
    }
}

手机号屏蔽&&获取身份证信息

package test5;

import java.util.Scanner;

public class phone {
    public static void main(String[] args) {
        System.out.println("请输入你的手机号码:");
        Scanner sc = new Scanner(System.in);
        String phoneNumber = sc.next();

        System.out.println("\n请输入你的身份证号码:");
        String identity = sc.next();


        String phone1 = phoneNumber.substring(0, 3);
        String phone2 = phoneNumber.substring(7);

        String result = phone1 + "****" + phone2;

        System.out.println("\n处理后手机号为:"+result);

        System.out.println("\n人物信息为:");
        getIdentity(identity);
    }
    //定义获取身份信息方法
    public static void getIdentity(String str){
        String year = str.substring(6,10);
        String month = str.substring(10,12);
        String day = str.substring(12,14);

        System.out.println("出生日期为: "+year+"年"+month+"月"+day+"日");
        char gender = str.charAt(16);

        int genderNumber = gender - 48;
        if(genderNumber % 2 == 0){
            System.out.println("性别为:女");
        }else{
            System.out.println("性别为:男");
        }
    }
}

敏感词屏蔽

定义一个敏感词数组,如果用户输入的语言在数组中出现,则替换为*,这道题其实就是学习一下String类中的replace方法

例如: TMD替换为***

package test5;

import java.util.Scanner;

public class replace {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //定义脏话数组:CNM,TMD,SB,傻逼,Sb,cnm,tmd
        String[] str1 = {"CNM", "TMD", "SB", "C", "傻逼", "Sb", "cnm", "tmd", "MLGB", "mlgb"};
        while(true) {
            System.out.println("请输入你想说的话: ");
            String str = sc.next();
            for (int i = 0; i < str1.length; i++) {
                str = str.replace(str1[i], "***");
            }
            System.out.println("\n处理后为: " + str);
        }
    }
}