zoukankan      html  css  js  c++  java
  • java04

    字符串加密

    1设计思想:

    改程序是对小写的a到z进行加密,输入一段字符串str,输入加密的密匙k,判断录入的的字符与

    ‘z’-k+1的大小,比其小的直接加上密匙转化为新的字符,大于的加(k-26)将最后几位字符转到

    开头,实现字符串的加密,解密反之。

    2程序流程图:

    源代码

    import java.util.Scanner;
    public class 加密 {

    public static void main(String[]args) {
    Scanner in1=new Scanner(System.in);
    System.out.println("请输入caesar cipher:");
    int key=in1.nextInt();
    System.out.println("解密请输入1,加密请输入2");
    Scanner in2=new Scanner(System.in);
    int i2=in2.nextInt();
    char[]t=new char[100];
    char[]f=new char[100];
    Scanner in3=new Scanner(System.in);
    String str;
    str=in3.nextLine();
    switch(i2)
    {
    case 1:{
    for(int i=0;i<str.length();i++)
    {
    t[i]=str.charAt(i);
    f[i]=(char) (t[i]-key);
    System.out.println(f[i]);
    }
    }break;
    case 2:{
    for(int i=0;i<str.length();i++)
    {
    f[i]=str.charAt(i);
    t[i]=(char) (f[i]+key);
    System.out.println(t[i]);
    }
    }break;
    }
    }
    }

    结果截图

    1..String.equals()方法的实现代码

    public boolean equals(Object anObject) {

            if (this == anObject) {

                return true;

            }

            if (anObject instanceof String) {

                String anotherString = (String)anObject;

                int n = value.length;

                if (n == anotherString.value.length) {

                    char v1[] = value;

                    char v2[] = anotherString.value;

                    int i = 0;

                    while (n-- != 0) {

                        if (v1[i] != v2[i])

                            return false;

                        i++;

                    }

                    return true;

                }

            }

            return false;

        }

    2.

    Length():获取字符串的长度

    例子:

    charAt():获取指定位置的字符

    例子:

    getChars():获取从指定位置起的子串复制到字符数组中它有四个参数,1.被拷贝字符在字串中的起始位置  2.被拷贝的最后一个字符在字串中的下标再加1  3.目标字符数组  4.拷贝的字符放在字符数组中的起始下标

    列子:

    replace():子串替换,可以将原字符串中的某个字符替换为指定的字符,并得到一个新的字符串,该方法的具体定义如下:

    toUpperCase()、 toLowerCase():大小写转换,在String类中提供了两个用来实现字母大小写转换的方法,它们的返回值均为转换后的字符串,

    其中toLowerCase()用来将字符串中的所有大写字母改为小写字母,,方法toUpperCase()用来将字符串中的所有小写字母改为大写字母

    trim():去除字符串的首尾空格,该方法的具体定义如下:


    toCharArray():将字符串对象转换为字符数组

    3请阅读JDK中String类上述方法的源码,模仿其编程方式,编写一个MyCounter类,它的方法也支持上述的“级联”调用特性

    复制代码
    class MyCounter
    {
        public int x;
        MyCounter(int xy)
        {
            this.x=xy;
        }
        MyCounter increase(int a)
        {
          this.x=this.x+a;
          return this;
        }
        MyCounter decrease(int b)
        {
            this.x =this.x-b;
            return this;
        }
    }
    public class ceshi_2 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyCounter counter1=new MyCounter(1);
            MyCounter counter2=counter1.increase(100).decrease(2).increase(3);
            System.out.println(counter2.x);
        }
    }
  • 相关阅读:
    第11组 团队Git现场编程实战
    团队项目-需求分析报告
    团队项目-选题报告
    第10组 Alpha事后诸葛亮
    第10组 Alpha冲刺(6/6)
    第10组 Alpha冲刺(5/6)
    第10组 Alpha冲刺(4/6)
    第10组 Alpha冲刺(3/6)
    第10组 Alpha冲刺(2/6)
    第10组 Alpha冲刺(1/6)
  • 原文地址:https://www.cnblogs.com/lxy10375/p/7743828.html
Copyright © 2011-2022 走看看