動手動腦6,動腦6

來源:互聯網
上載者:User

動手動腦6,動腦6

一、古羅馬皇帝凱撒在打仗時曾經使用過以下方法加密軍事情報:

請編寫一個程式,使用上述演算法加密或解密使用者輸入的英文字串要求設計思想、程式流程圖、原始碼、結果。

1、程式設計思想:從鍵盤輸入任一字元串ming,每個字元往後挪3,替換成mi,用迴圈演算法,最終一個一個字元按序輸出。

2、程式流程圖:

3、原始碼:

import java.util.Scanner;public class My2 {    public static void main(String[] args)    {         Scanner in = new Scanner(System.in);       System.out.print("請輸入明文: ");       String ming = in.nextLine();       in.close();       String r="";       char mi;       for(int i=0;i<ming.length();i++)       {           mi=(char)(ming.charAt(i)+3);           r=r+mi;                 }      System.out.println("密文為:"+r);    }}

結果:

二、String.equals()方法、整理String類的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用說明、閱讀筆記。

1、String.equals()方法

   用來可以比較兩個字串的內容。而使用==比較兩字串變數是否引用同一字串對象。

   使用方法;

     (1)String s1=new String("Hello");

   String s2=new String("Hello");

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

      比較構造方法的初始化對象的內容。

     (2)String s3="Hello";   

   String s4="Hello";

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

      比較兩個字串的內容。

   2、Length()

擷取字串長度(字串內包含的字元個數)。

使用方法:

(1)、String s1 = new String( "hello" );

     System.out.println(s1.length());//6

(2)、String s3="Hello";

          System.out.println(s3.length());//6

   3、charAt()

擷取指定位置的字元。第一個字元是從0開始的。

使用方法:

   String s3="Hello";

   System.out.println(s1.charAt(1));//e

   4、getChars(a,b,c,d)

擷取從指定位置起的子串複製到字元數組中,有四個參數:

a.被拷貝字元在字串中的起始位置(整型數字)

    b.被拷貝的最後一個字元在字串中的下標再加1(整型數字)

    c.目標字元數組(數組名稱)

d.拷貝的字元放在字元數組中的起始下標(整型數字)

使用方法:

      String s1, output;

      char charArray[];

      s1 = new String( "hello there" );

  charArray = new char[ 5 ];

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

  output += "\nThe character array is: ";//The character array is:hello

   5、replace()

     子串替換,replace(char oldChar,char newChar):將字串中的所有的oldChar字元替換為newChar字元。

     使用方法:

String s1=”aBC123”;

       System.out.println(s1.replace(‘C’,’0’));//將所有的‘C’替換為‘0’

     輸出結果為:aB0123

   6、toUpperCase()

     將(當前)字串轉換為大寫(英文)。

     使用方法:

       String s1=”abC123”;

       System.out.println(s1.toUpperCase());//將當前字串轉換為大寫

     輸出結果:ABC123

   7、toLowerCase()

     將(當前)字串轉換為小寫(英文)。

      使用方法:

       String s1=”aBC123”;

       System.out.println(s1.toLowerCase());//將當前字串轉換為小寫

      輸出結果:abc123

   8、trim()

      去除字串頭尾空格。

使用方法:String s1=”a b c”

          System.out.println(s1.trim());//去除字串頭尾空格

輸出結果:a b c

  9、toCharArray()

     將字串對象轉換為字元數組(空格算一個字元)

使用方法:

   String s1=”Java”;

   char[] s2=s1.toCharArray();

   System.out.println(s2);

輸出結果:Java

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.