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

 

posted on 2016-10-28 18:36  Macsad  阅读(106)  评论(0编辑  收藏  举报