zoukankan      html  css  js  c++  java
  • String随笔

    1、古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:,请编写一个程序,使用上述算法加密或解密用户输入的英文字串要求设计思想、程序流程图、源代码、结果截图。

    设计思想:1)定义一个String类型,输入一个字符串,接着在定义一个空的字符串st;

                  2)定义一个char m,使用charAt()函数来提取相应的字符,如果m不等于xyz或者XYZ,则m=(char)(m+3);

                  3)如果m=xyz或者XYZ,则对应输出abc或者ABC;

                  4)st=st+m;输出加密后的字符串st。

    程序流程图:

                      

    源代码:
    import java.util.Scanner;
    public class lianxi {
        public static void main(String[] args){
           String s;
           Scanner in=new Scanner(System.in);
           System.out.print("请输入一段字符串:");
           s=in.next();
           String st=new String();
           char m ;
           for(int i=0;i<s.length();i++)
           {
            m=s.charAt(i);
            if(((m!='x')&&(m!='y')&&(m!='z'))&&((m!='X')&&(m!='Y')&&(m!='Z')))
            {
             m=(char)(m+3);
            }
            else if(m=='x') {m='a';}
            else if(m=='X') {m='A';}
            else if(m=='y') {m='b';}
            else if(m=='Y') {m='B';}
            else if(m=='z') {m='c';}
            else if(m=='Z') {m='C';}
            st=st+m;
             }
           System.out.println("加密后的字符串为:"+st);
       }      
    }

    程序截图:

    2、动手动脑之String.equals()方法、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明、阅读笔记发表到博客园。

    Java中String类的常用方法:

    public char charAt(int index)
    返回字符串中第index个字符;
    public int length()
    返回字符串的长度;
    public int indexOf(String str)
    返回字符串中第一次出现str的位置;
    public int indexOf(String str,int fromIndex)
    返回字符串从fromIndex开始第一次出现str的位置;
    public boolean equalsIgnoreCase(String another)
    比较字符串与another是否一样(忽略大小写);
    public String replace(char oldchar,char newChar)
    在字符串中用newChar字符替换oldChar字符
    public boolean startsWith(String prefix)
    判断字符串是否以prefix字符串开头;
    public boolean endsWith(String suffix)
    判断一个字符串是否以suffix字符串结尾;
    public String toUpperCase()
    返回一个字符串为该字符串的大写形式;
    public String toLowerCase()
    返回一个字符串为该字符串的小写形式
    public String substring(int beginIndex)
    返回该字符串从beginIndex开始到结尾的子字符串;
    public String substring(int beginIndex,int endIndex)
    返回该字符串从beginIndex开始到endsIndex结尾的子字符串
    public String trim()
    返回该字符串去掉开头和结尾空格后的字符串
    public String[] split(String regex)
    将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

  • 相关阅读:
    Codeforces Round #251 (Div. 2) A
    topcoder SRM 623 DIV2 CatAndRat
    topcoder SRM 623 DIV2 CatchTheBeatEasy
    topcoder SRM 622 DIV2 FibonacciDiv2
    topcoder SRM 622 DIV2 BoxesDiv2
    Leetcode Linked List Cycle II
    leetcode Linked List Cycle
    Leetcode Search Insert Position
    关于vim插件
    Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones
  • 原文地址:https://www.cnblogs.com/th1314/p/6004963.html
Copyright © 2011-2022 走看看