【Java实验五】字符串加密、模拟用户登录、生成验证码、春节倒计时等

🏠个人主页:黑洞晓威
🧑个人简介:大家好,我是晓威,一名普普通通的大二在校生,希望在CSDN中与大家一起成长。🎁如果你也在正在学习Java,欢迎各位大佬来到我的博客查漏补缺呀,如果有哪里写的不对的地方也欢迎诸佬指正啊。

实验内容

1、实验题目:字符串加密

键盘输入一个原始字符串作为明文,然后使用加密方法加密,再对加密字符串进行解密。样例如下图,加密方法自定,完成其功能并测试。

源代码:

public static void main(String[] args) {
   Scanner scanner = new Scanner(System.*in*);
   System.*out*.println("请输入明文");
   System.*out*.println("*****************************");
   System.*out*.println("加密方法:每个字符的ASCLL码加1");
   System.*out*.println("*****************************");
   char[] chars;//实例化一个字符串数组用于加密与解密
   String str = null;//实例化一个字符串用于输入明文
   if(scanner.hasNext()){
     str = new String(scanner.next());//得到明文
   }
   scanner.close();
 
   chars = str. toCharArray();//将明文从String转化为char[]数组
   for(int i = 0;i<chars.length;i++){
     chars[i]+=1;//将字符串数组中的每一个元素加一,即ASCLL码加一
   }
   String str2 = new String(chars);//再将char[],转化为String字符串输出
   System.*out*.println("密文:"+str2);
   System.*out*.println("*****************************");
 
   //解密
   for(int i =0;i<chars.length;i++){
     chars[i]-=1;//同理,再将每个ASCLL减一
   }
   String str3 = new String(chars);//转化为字符串
   System.*out*.println("解密:"+str3);
 }

 

列出测试数据和实验结果截图:

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

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

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

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

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

源代码:

public static void main(String[] args) {
    System.out.println("欢迎打开计算器的小实验o(* ̄▽ ̄*)ブ");
    Scanner scanner = new Scanner(System.in);
    String user = null;//用户名
    String password = null;//密码
    
        System.out.println("发现您还没有注册快来注册吧(≧▽≦*)o");
        System.out.println("请注册您的账号");
         user = scanner.next();
         
        boolean same = false;//作为两次密码是否一致的标识
        System.out.println("请设置您的密码");
         password = scanner.next();
        System.out.println("请再次输入您的密码");
        String password2 =scanner.next();
        if(password.equals(password2)){//如果密码一致
            same = true;//两次密码一致
            System.out.println("注册成功!");
        }
        while (!same){//如果两次密码不一致。重新输入
            System.out.println("两次输入的密码不一致哦,请重新输入密码");
            password = scanner.next();
            System.out.println("再次输入密码");
            password2 = scanner.next();
            if(password.equals(password2)){
                same = true;//标识为true跳出循环
                System.out.println("终于注册成功啦");
            }
        }
    

    int count = 3;//给定的三次机会
    while (count>0){//循环登陆
        System.out.println("请进行登录(☆▽☆)");
        System.out.println("输入用户名");
        String myuser = scanner.next();
        System.out.println("请输入密码");
        String mypasswprd = scanner.next();
        
        //如果用户名密码正确
        if(myuser.equals(user) && mypasswprd.equals(password)){
            System.out.println("登录成功!即将打开计算器!");
            try {
                //打开计算器
                Process pro = Runtime.getRuntime().exec("calc " );
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }else {
            System.out.println("账号或密码错误,请重新登录!");
            count--;//机会减一
        }
    }
    //如果三次都失败
    if(count==0){
        System.out.println("登录失败,无法再登录了/(ㄒoㄒ)/~~");
    }
      scanner.close();
}

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

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

源代码:

public static void main(String[] args) {
    //记录每种字符的个数,初始化为0;
    int big = 0,small = 0,num = 0,other = 0;
    String str = null;
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入一串字符");
    if (scanner.hasNext()){
        str = scanner.next();
    }

    //将字符串转化为char[]数组方便统计每一个字符
    char[] chars = str.toCharArray();
    for(char ch : chars){
        if(ch>=48 && ch <=57){
            num++;//数字
        }else if(ch>=65 && ch<=90){
            big++;//大写字母
        }else if(ch>=97 && ch<=122){
            small++;//小写字母
        }else {
            other++;//其他
        }
    }
    System.out.printf("数字有:%d个,小写字母有%d个,大写字母有%d个,其他符号有%d个",num,small,big,other);
}

列出测试数据和实验结果截图:

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

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

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

源代码:

public class S5_4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串");
        StringBuffer strb = new StringBuffer(scanner.next());
        
        reverse(strb);
        System.out.println("反转后为:"+strb);

        String[] arr = new String[]{"Hello","Word"};
        String str = strcomb(arr);
        System.out.println("String拼接后:"+str);

        StringBuffer sbuff = buffcomb(arr);
        System.out.println("StringBuffur拼接后:"+sbuff);
    }
    //定义反转方法
    public static void reverse(StringBuffer strb){
        int n = strb.length();//获得字符串长度
        for(int i = 0;i<n/2;i++){
            //首位元素互换
            char ch =strb.charAt(i);
            char chl =strb.charAt(n-1-i);
            strb.setCharAt(i,chl);
            strb.setCharAt(n-1-i,ch);
        }
    }
    //定义String拼接方法
    public static String strcomb(String[] arr){
        String str = new String();
        for(String s : arr){//增强for循环遍历
            str+=s;//拼接
        }
        return str;
    }

    //定义StringBuffer拼接方法
    public static StringBuffer buffcomb(String[] arr){
        StringBuffer sbuff = new StringBuffer();
        for(String str : arr){
            sbuff.append(str);//拼接
        }
        return sbuff;
    }

}

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

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

源代码:

public static void main(String[] args) {
    int num = 1234567890;
    String s ="qwertyuiopasdfghjklzxcvbnm";
    String S=s.toUpperCase();
    String word=s+S+num;
    //先定义一个字符串数组,其中包含数字以及大小写的字母
    char[] chars = word.toCharArray();

    Random random = new Random();
    String str = "";//验证码
    for(int i = 0;i<6;i++){//循环六次
        //随机生成一个int 属于[0,chars.length),正好匹配字符串数组的下标
        int index = random.nextInt(chars.length);
        str += chars[index];//将chars[index]拼接到str
    }
    System.out.println("生成的验证码为:"+str);

    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入验证码");
    String in = scanner.next();
    //注意忽略大小写
    if(str.compareToIgnoreCase(in)==0){
        System.out.println("恭喜验证成功!");
    }else System.out.println("验证失败");
}

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

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

源代码:

public static void main(String[] args) {
    //获得当前的事件
    LocalDateTime now = LocalDateTime.now();
    //获得新年的时间
    LocalDateTime newyear = LocalDateTime.of(2023, 1, 1, 0, 0);

    //获得相应时间距离1970年的秒数
    long time1 = now.toEpochSecond(ZoneOffset.ofHours(0));
    long time2 = newyear.toEpochSecond(ZoneOffset.ofHours(0));

    //计算相差多少天
    long day = (time2-time1)/60/60/24;
    System.out.printf("距离兔年春节还有%d天",day);
}

列出测试数据和实验结果截图:

7、实验题目:二月天

源代码:

public static void main(String[] args) {
    System.out.println("请输入年份");
    Scanner scanner = new Scanner(System.in);
    int year = scanner.nextInt();

    //闰年判断条件:1.能被4整除但不能被100整除
    //            2.能被400整除
    if(year%4==0 && year%100!=0 || year%400==0){
        System.out.println("这一年的二月只有28天诶");
    }else {
        System.out.println("这一年的二月有29天呢");
    }
}

8、实验题目:正则表达式。(选做)

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

源代码:

public static void main(String[] args) {
    String str = new String("中华人民共和国成立于1949年10月1日");
    // 定义一个模式,其中正则表达式为非数字
    Pattern pattern = Pattern.compile("[^0-9]");
    //找到str种所有符合正则表达式的元素,即非数字元素
    Matcher matcher = pattern.matcher(str);
    //将非数字的元素全部替换为“”,即最后只剩下了数字
    String num = matcher.replaceAll("");
    System.out.println("提取的数字为:"+num);
} 

友情提示:

代码仅供大家学习与参考,要想提升自己一定要自己亲自敲一边代码哦,让我们一起努力吧🍭

🎉文章到这里就结束了,感谢诸佬的阅读。🎉

💕欢迎诸佬对文章加以指正,也望诸佬不吝点赞、评论、收藏加关注呀😘

  • 12
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑洞晓威

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

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

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

打赏作者

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

抵扣说明:

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

余额充值