• String 归档


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

    设计思想:

    1)输入一个字符串str(需要加密的),在定义一个字符串result(加密后的)

    2)str.length()得出字符串长度,for循环length次

    3)每次循环 每个字符+3(加密过程)

    源代码:

    import java.util.Scanner;
    public class q {
         public static void main(String[] args){
          System.out.print("输入需要加密的文字");
          String str,result;
          Scanner sc=new Scanner(System.in);
          result=new String();
          str=sc.next();//输入待加密字符串
          sc.close();
          int length;
          length=str.length();
          char temp;
          for(int i=0;i<length;i++){
           temp=str.charAt(i); 
                if(((temp!='x')&&(temp!='y')&&(temp!='z'))&&((temp!='X')&&(temp!='Y')&&(temp!='Z')))
            {
             temp=(char)(temp+3);
            }
            else if(temp=='x') {temp='a';}
            else if(temp=='X') {temp='A';}
            else if(temp=='y') {temp='b';}
            else if(temp=='Y') {temp='B';}
            else if(temp=='z') {temp='c';}
            else if(temp=='Z') {temp='C';}   
                  result +=temp;
          }
          System.out.print("加密后:"+result);
         }
     }

    实验截图:

    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)
    将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

  • 相关阅读:
    乱谈数学--傅里叶变换(级数)的原理(一)
    Linux day03
    Linux day02
    Linux day01(二)虚拟机快照和克隆的用法介绍
    Linux day01(一) 创建Linux虚拟机,设置虚拟机默认属性,虚拟机和Xhell建立连接
    Django day 38 结算中心,支付中心,计算价格方法
    Django day 37 网站视频的播放,购物车接口,优惠券表分析
    Django day 36 支付宝支付,微信推送
    Django day35 redis连接池,redis-list操作,django中使用redis,支付宝支付
    Django day 34 过滤课程,登录,redis,python操作redis
  • 原文地址:https://www.cnblogs.com/dk1203573488/p/6008981.html
Copyright © 2020-2023  润新知