ke tang zuo ye

思想

程序的功能是用户输入一段字符串之后,用户可以选择进行加密或解密。然后输入加密或解密时需要移动的位数。调用String.charAt(index)方法改变每一个字符,最后输出。

程序流程图

 

 
   

 

 

 

源代码

import java.util.Scanner;

public class Kaisa {

       String message;

       public void go(){

              Scanner sc=new Scanner(System.in);

              System.out.println("输入字符串");

              message=sc.nextLine();

              System.out.println("输入密码");

              int key=sc.nextInt();

              System.out.println("请选择:\n1 加密\n2 解密");

              int choose=sc.nextInt();

              String newM="";

              if(choose==1){

                     char c;

                     for(int i=0;i<message.length();i++){

                            c=message.charAt(i);

                            if(c>90-key&&c<97||c>122-key){

                                          c-=26;

                                   }

                                   c+=key;

                            newM+=c;

                     }

                     System.out.println(newM);

              }

              else if(choose==2){

                     char c;

                     for(int i=0;i<message.length();i++){

                            c=message.charAt(i);

                            if(c>=97&&c<97+key||c<65+key){

                                   c+=26;

                            }

                            c-=key;

                            newM+=c;

                     }

                     System.out.println(newM);

              }

              else System.out.println("没有这个选项");

       }

       public static void main(String[]args){

              Kaisa k=new Kaisa();

              k.go();

       }

}

结果截图

 

       
   

 

   

 

 

 


课后作业2

动手动脑

    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;

}

可以看出,首先它是判断传过来这个对象跟调用这个方法的对象是不是是同一个对象,如果是,那么返回true。紧接着判断两个字符串长度是否相等,若相等,把他们保存在两个数组里,然后挨个比较。

 

2.通过查询JDK(在方法处按住ctrl,把鼠标移到上面,根据提示可以打开)。我发现String连续调用的方法的做法是每次返回一个String对象。于是我写了一下代码:

public class MyCounter {

       private int value;

       public int get()

       {

              return this.value;

       }

       public MyCounter()

       {}

       public MyCounter(int avalue)

       {

              value=avalue;

       }

       public MyCounter increase(int avalue)

       {

              return new MyCounter(value+avalue);

       }

       public MyCounter decrease(int avalue)

       {

              return new MyCounter(value-avalue);

       }

       public static void main(String[] args)

       {

              MyCounter counter1=new MyCounter(1);

              MyCounter counter2=counter1.increase(100).decrease(2).increase(3);

              System.out.println(counter2.get());

       }

}

运行结果:102

 

posted @ 2017-10-27 17:57  瓜大wjs  阅读(167)  评论(0编辑  收藏  举报