zoukankan      html  css  js  c++  java
  • Java第四次上课博文动手动脑

    Java第四次上课博文动手动脑

    1.    查看String.equals()方法

    public class StringEquals {

       

    /**

         * @param args the command line arguments

         */

       

          public static void main(String[] args) {

           

               String s1=new String("Hello");

           

               String s2=new String("Hello");

           

               System.out.println(s1==s2);

           

               System.out.println(s1.equals(s2));

           

               String s3="Hello";

           

               String s4="Hello";

             

               System.out.println(s3==s4);

           

               System.out.println(s3.equals(s4));

           

       

          }

    }

    equals 方法(是String类从它的超类Object中继承的)被用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写。在实际使用的过程中,我们可以重写equals()

    方法。

    2.连续调用类的方法

    //编写程序实现多级调用

    //王宏伟,2015,10,25

     

    package test;

     

    public class MyCount

    {

     

      public static void main(String args[])

      {

         MyCount mycount1 = new MyCount(23);

         MyCount mycount2 = mycount1.increase(100).decrease(3).increase(21);

         System.out.println(mycount2.num);

      }

     

      private int num;

     

      public MyCount(int a)//构造方法

      {

         num = a;

      }

     

     

      public MyCount  increase(int a)//increase方法

      {

         num = num + a;

         return this;

      }

     

     

     

      public MyCount  decrease(int a)//decrease方法

      {

         num = num - a;

         return this;

      }

    }

    2.    古罗马皇帝英文字符串加密:

    程序设计思想:

    本程序在用户的输入与系统的输出方面采用系统对话框的形式来完成。首先要对用户的输入进行判断,判断其输入是否合法。如果不合法就强行结束程序并进行错误提示。如果合法,首先要把用户输入的字符串转化为char数组,求出char的长度,然后用一个for循环来完成对char数组中每个字符的挨个替换。对于处在字典顺序中间字母可直接向前或者是向后移动3位,但是像处于最前面的abc和最后面的xyz就要单独处理。同理还有ABC和XYZ。

    程序源代码:

    //古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:

    //请编写一个程序,使用上述算法加密或解密用户输入的英文字串。

    //王宏伟,2015,10,25

     

    package test;

     

    import javax.swing.*;

     

    public class KingPassword

    {

      public static void main(String[] args)

      {

         String inputString;//用户输入

         int choice;

         choice = Integer.valueOf(JOptionPane.showInputDialog( "对字符串加密输入1,对字符串解密输入2" ));

         //把用户输入的字符串1或2转换为整型1或2

         if(choice == 1)

         {

           inputString = JOptionPane.showInputDialog( "请输入要加密的字符串:" );

           char a[] = inputString.toCharArray();

           int length = inputString.length();//求出字符数组的长度

           for(int i = 0;i < length;i++)

           {

             if(a[i] == 'x')

                a[i] = 'a';

             else if(a[i] == 'y')

                a[i] = 'b';

             else if(a[i] == 'z')

                a[i] = 'c';

             else if(a[i] == 'X')

                a[i] = 'A';

             else if(a[i] == 'Y')

                a[i] = 'B';

             else if(a[i] == 'Z')

                a[i] = 'C';

             else

                a[i] += 3;

           }//对字符串中的每个字符依次加密

           String outputString = new String(a);//把字符数组再转回字符串

          

            JOptionPane.showMessageDialog( null, outputString,

                  "加密后的字符串为",

                    JOptionPane.INFORMATION_MESSAGE );

            //使用对话框来显示加密后的字符串

         }

        

         else if(choice == 2)

         {

           inputString = JOptionPane.showInputDialog( "请输入要解密的字符串:" );

           char a[] = inputString.toCharArray();//把字符串转化为字符数组

           int length = a.length;//求出字符数组的长度

           for(int i = 0;i < length;i++)

           {

             if(a[i] == 'a')

                a[i] = 'x';

             else if(a[i] == 'b')

                a[i] = 'y';

             else if(a[i] == 'c')

                a[i] = 'z';

             else if(a[i] == 'A')

                a[i] = 'X';

             else if(a[i] == 'B')

                a[i] = 'Y';

             else if(a[i] == 'C')

                a[i] = 'Z';

             else

                a[i] -= 3;//每个字符后移三位

            

           }//对字符串中的每个字符依次加密

           String outputString = new String(a);

            JOptionPane.showMessageDialog( null, outputString,

                    "解密后的字符串为",

                    JOptionPane.INFORMATION_MESSAGE );

            //使用对话框来显示加密后的字符串

         }

        

         else

         {

            JOptionPane.showMessageDialog( null, "输入有误",

                    "错误提示!",

                    JOptionPane.INFORMATION_MESSAGE );

         }//对非法输入有错误提示

      }

    }

    程序运行结果截图:

     

  • 相关阅读:
    m_Orchestrate learning system---三十一、模板和需求的关系
    你如何破解后安装PS cs6
    STL 源代码分析 算法 stl_algo.h -- pre_permutation
    霸气侧漏HTML5--之--canvas(1) api + 弹球例子
    hello nodejs
    整洁的正则表达式教程,这很容易理解
    基于Gsoap 的ONVIF C++ 库
    一个常见的错误时编写代码
    IT只忍者龟Photoshop简单人像的头发抠图过程
    彩色图像--色彩空间 HSI(HSL)、HSV(HSB)
  • 原文地址:https://www.cnblogs.com/1102whw/p/4909890.html
Copyright © 2011-2022 走看看