Java程序设计课程——实验5

1、实验题目:模拟用户登录

编写一个程序,模拟用户登录。程序要求如下:

(1)用户名和密码正确(不区分大小写),提示“登录成功”,并打开Windows的计算器程序;

(2)用户名或密码不正确,提示“用户名或密码错误”;

(3)总共有3次登录机会,超过3次,则提示“登录失败,无法再继续登录”。

public class S5_2 {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        String inputUsername, inputPassword;
        User user = new User();
        int i = 0;//用于控制用户的登录机会数
        while(i < 3){
            System.out.println("请输入用户名:");
            inputUsername = in.nextLine();
            System.out.println("请输入密码:");
            inputPassword = in.nextLine();
            System.out.println(user.getUsername());
            if (inputUsername.equals(user.getUsername()) && inputPassword.equals(user.getPassword())){
                System.out.println("登录成功");
                //调用计算机程序
                Runtime.getRuntime().exec("cmd /c start C:\\WINDOWS\\system32\\calc.exe");
                break;
            } else {
                System.out.println("用户名或密码错误");
            }
            i++;
        }
        if (i == 3){
            System.out.println("登陆失败,无法再继续登录");
        }
    }
}

class User{
    //考虑到用户的信息安全,这里使用private修饰,要提供get方法
    private String username = "Spraing";
    private String password = "duanboy";

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

2、实验题目:统计字符个数

从键盘输入一个字符串,分别统计该字符串中所有大写字母、小写字母、数字、其它字符的个数,并分类输出这些字符和统计结果。(提示:不考虑字符内容,例如:Hello123World,大写2个,小写8个,数字3个。)

public class S5_3 {
    public static void main(String[] args) {
        /**
         * 键盘录入一个字符串,统计该字符串中
         * 大小写和数字的字符个数
         * 大写字母  ch>='A' && ch <='Z'
         * 小写字母 ch >='a' && ch<='z'
         * 数字 ch >='0' && ch<='9'
         */
        Scanner in = new Scanner(System.in);
        String string;
        int bigcount = 0;
        int smallcount = 0;
        int numcount = 0;
        int elsecount = 0;
        System.out.println("请输入一个字符串:");
        string = in.nextLine();
        for (int i = 0; i < string.length(); ++i){
            char ch = string.charAt(i);
            if (ch>='A' && ch <='Z'){
                bigcount++;
            } else if (ch >='a' && ch<='z')
            {
                smallcount++;
            } else if (ch >='0' && ch<='9'){
                numcount++;
            } else {
                elsecount++;
            }
        }
        System.out.println("大写字母:"+bigcount);
        System.out.println("小写字母:"+smallcount);
        System.out.println("数字:"+numcount);
        System.out.println("其他字符:"+elsecount);
    }
}

3、实验题目:字符串缓冲区练习

(1)使用StringBuffer类对键盘输入的字符串进行反转。

(2)使用String和StringBuffer类分别对数组进行字符串拼接,使其变成一个字符串。

public class S5_4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String string;
        //使用StringBuffer类对键盘输入的字符串进行反转
        System.out.println("请输入一个字符串:");
        string = in.nextLine();
        StringBuffer sb = new StringBuffer(string);
        System.out.println("反转之后的字符串为:");
        System.out.println(sb.reverse().toString());
        //使用String对数组进行字符串拼接,使其变成一个字符串
        int arr[] = {3,5,11,23,12};
        String str = "";
        for (int i = 0; i < arr.length; ++i){
            str += arr[i];
        }
        System.out.println("拼接之后的数组为:"+str);
        //使用StringBuffer对数组进行字符串拼接,使其变成一个字符串
        StringBuffer sb1 = new StringBuffer();
        for(int i = 0; i < arr.length; ++i){
            sb1.append(arr[i]);
        }
        System.out.println("拼接之后的数组为:"+sb1.toString());
    }
}

4、实验题目:生成验证码

使用Random类创建一个6位的验证码,其中包含数字、字母的组合,并通过键盘输入该验证码,验证通过(不区分大小写)时提示“恭喜验证成功!”,否则提示“验证失败!”。

public class S5_5 {
    public static void main(String[] args) {
        Allcharacter all = new Allcharacter();
        Scanner in = new Scanner(System.in);
        Random random = new Random();
        String code = "";
        String verification = "";
        for (int i = 0; i < 6; ++i){
            //随机生成六次索引,然后对验证码进行拼接
            int index = random.nextInt(58);
            code += all.allcharacter.charAt(index);
        }
        System.out.println("验证码为:");
        System.out.println(code);
        System.out.println("请输入验证码:");
        verification = in.nextLine();
        if (verification.equalsIgnoreCase(code) ){
            System.out.println("恭喜验证成功");
        } else {
            System.out.println("验证失败");
        }
    }
}

class Allcharacter{
   public String minuscule = "abcdefghigklmnopqrstuvwxyz";
   public String majuscule = minuscule.toUpperCase();
   public String num = "0123456789";
   public String allcharacter = minuscule+majuscule+num;
}

5、实验题目:春节倒计时

根据所学知识,计算明年(兔年)春节的倒计时天数并输出:“距离兔年春节还有***天”。

public class S5_6 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //字符串变为时间Date类,解析p格式化f,pf
        String newyear = "2023-1-20 12:00:00";
        Date d = sdf.parse(newyear);
        //获得时间毫秒值
        long yeartime = d.getTime();
        //当前日期毫秒值,需要转换为为天数
        long currentTime = new Date().getTime();
        System.out.println("距离兔年春节还有:"+(yeartime - currentTime)/1000/60/60/24+"天");
    }
}

6、实验题目:二月天

二月是一个有趣的月份,平年的二月有28天,闰年的二月有29天。编写一个程序,从键盘输入年份,根据输入的年份计算这一年的二月有多少天。

public class S5_7 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int year;
        System.out.println("请输入你要查询的年份:");
        year = in.nextInt();
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
            System.out.println("该年份的二月份有29天");
        } else {
            System.out.println("该年份的二月份有28天");
        }
    }
}

7、实验题目:正则表达式。

“中华人民共和国成立于1949年10月1日”,利用正则表达式提取出其中的数字。

public class S5_8 {
    public static void main(String[] args) {
        String content = "中华人民共和国成立于1949年10月1日";
        //正则表达式,用于匹配非数字串,+号用于匹配出多个非数字串
        String regEx="[^0-9]+";
        Pattern pattern = Pattern.compile(regEx);
        //用定义好的正则表达式拆分字符串,把字符串中的数字留出来
        String[] cs = pattern.split(content);
        System.out.println(Arrays.toString(cs));
    }
}

扩展:

用正则表达式提取其中的汉字

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spraing※boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值