Java第三次課程作業

來源:互聯網
上載者:User

標籤:+=   ack      string   str   system   ext   選擇   int()   

一.字串加密

1.設計思想

輸入英文字串,根據加密或解密密碼將英文字串進行逐個加密或解密,最後輸出加密或解密後的字串

2.程式流程圖

3.原始碼

package kaisa;
import java.util.Scanner;
public class kaisa
{
public static void main(String[] args)
{
System.out.println("請選擇:1.加密 2.解密");
Scanner c=new Scanner(System.in);
String ss=c.nextLine();
if(ss.equalsIgnoreCase("1"))//if語句判斷加密還是解密
{
System.out.println("請輸入要加密的英文字串");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println("請入加密密碼");
Scanner sc1=new Scanner(System.in);
int key=sc1.nextInt();
jiami(s,key);
}
else if(ss.equalsIgnoreCase("2"));
{
System.out.println("請輸入要解密的英文字串");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println("請輸入解密密碼");
Scanner sc1=new Scanner(System.in);
int key=sc1.nextInt();
jiemi(s,key);
}
}

public static void jiami(String str,int k)//加密函數
{
String string="";
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
if(c>=‘a‘&&c<=‘z‘)
{
c+=k%26;
if(c<‘a‘)
c+=26;
if(c>‘z‘)
c-=26;
}
else if(c>=‘A‘&&c<=‘Z‘)
{
c+=k%26;
if(c<‘A‘)
c+=26;
if(c>‘Z‘)
c-=26;
}
string+=c;
}
System.out.println(str+"加密後為:"+string);
}

public static void jiemi(String str,int n)//解密函數
{
int k=Integer.parseInt("-"+n);
String string="";
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
if(c>=‘a‘&&c<=‘z‘)
{
c+=k%26;
if(c<‘a‘)
{
c+=26;
}
if(c>‘z‘)
{
c-=26;
}
else if(c>=‘A‘&&c<=‘Z‘)
{
c+=k%26;
if(c<‘A‘)
{
c+=26;
}
if(c>‘Z‘)
{
c-=26;
}
string+=c;
}
System.out.println(str+"加密後為:"+string);
}
}

4.結果

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.