zoukankan      html  css  js  c++  java
  • Java课后作业04

    一、古罗马皇帝凯撒在打仗时曾经加密军事情报:

    1、设计思想:

    加密原理是abc等全部后移3xyz分别等于abc,根据ascii码表的转化,将其利用charat()取单个字符进行转化,再利用StringBuffer类存储输出。

    2、流程图

    3、源代码

     1 import java.util.Scanner;
     2 public class StringCode {
     3     
     4     
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         String n=null;  //初始化字符串
    11         Scanner input=new Scanner(System.in);
    12         System.out.println("请输入需要加密的字符串:");
    13         n=input.next();  //输入
    14         StringBuffer buf = new StringBuffer();  
    15         for(int i=0;i<n.length();i++){
    16         char x=n.charAt(i);  //取值
    17         if(n.charAt(i)>='A'&&n.charAt(i)<='W'){
    18             x=(char)(x+3);
    19             buf.append(x);   //A-W
    20             }
    21         else if(n.charAt(i)>='a'&&n.charAt(i)<='w')
    22             {
    23             x=(char)(x+3);
    24             buf.append(x);  //a-w
    25         }
    26         else if(n.charAt(i)>='x'&&n.charAt(i)<='z'){
    27             x=(char)(x-23);
    28             buf.append(x); //x-z
    29         }
    30         else if(n.charAt(i)>='X'&&n.charAt(i)<='Z'){
    31             x=(char)(x-23);
    32             buf.append(x);  //X-Z
    33         }
    34         }
    35         
    36         System.out.println("加密后的字符串:");
    37         System.out.println(buf.toString()); //输出加密后的
    38          System.exit( 0 );
    39     }
    40     
    41 }

    4 结果

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

        

       public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                return false;
            }
            return true;
            }
        }
        return false;
    }

    三、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Length():获取字串长度

    String a=”asdasd”;

    Int n=a.length();

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

    String a=”abc”;

    Char x=charAt(2);  则x=b

    getChars():获取从指定位置起的子串复制到字符数组中(它有四个参数)

    s1.getChars( 0, 5, charArray, 0 );

    /四个参数的含义

          //1.被拷贝字符在字串中的起始位置

          //2.被拷贝的最后一个字符在字串中的下标再加1

          //3.目标字符数组

    //4.拷贝的字符放在字符数组中的起始下标

    replace():子串替换

       String s=”\”;

            System.out.println(s.replace(“\”,”///”));

            结果///;

    toUpperCase()、 toLowerCase():大小写转换

    public String toUpperCase()//将字符串全部转换成大写

             System.out.println(new String(“hello”).toUpperCase());

    toLowerCse():public String toLowerCase()//将字符串全部转换成小写

             System.out.println(new String(“HELLO”).toLowerCase());

    trim():去除头尾空格:

    String x=”ax  c”;

             System.out.println(x.trim());//是去两边空格的方法

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

    char myChar[]=x.toCharArray();

              System.out.println(“myChar[1]”+myChar[1]);

  • 相关阅读:
    Linux安装phpMywind
    CentOS7安装virtualbox
    MySQL3534
    DIV盒子介绍
    HTML选择器
    人脸检测
    openblas下载安装编译
    DeepLearning网络设计总结
    Linux命令替换字符串
    Y7000联想拯救者gtx1050Ti安装cuda9.0
  • 原文地址:https://www.cnblogs.com/liulala2017/p/7729738.html
Copyright © 2011-2022 走看看