Judge 2-1

NCNU Programming 1042 Judge 2-1

西元前 49 年,羅馬共和國末期,動盪不安的社會,凱薩率軍佔領羅馬,打敗龐培,集大權於一身,實行獨裁統治。

在某次關鍵戰役裡,為了避免機密軍情走漏,凱薩便發明了一種特殊的加密法,使敵軍無法竊取情報,進而大敗敵軍。

這套神奇的加密法流傳下來,世人稱他為「凱薩加密法」。

西元 2016 年的今天,助教悅悅意外獲得時光機,決定要來惡搞這段歷史。而最好的方法就是,寫出一套破解凱薩加密法的程式,來幫助敵軍。

但是,要雷爆凱薩,光靠悅悅是不夠的。讓我們一起來惡搞屬於我們的歷史吧! ^.<

凱撒密碼的加密、解密方法簡單來說 首先將字母用數字代替,A=0,B=1,...,Z=25。 令 x字母 對照的數字 此時 偏移量n加密方法 即為: E(x) = (x + n) \mod 26 解密就是: D(x) = (x - n) \mod 26

輸入、輸出說明

  • 輸入一個正整數(int) key
  • 選擇 解密加密 輸入欲加解密的內容。
  • 下方 JLabel 會產生加密貨解密的結果。
  • 需要用把 String 切割成 charint 對應 ASCII。
  • 請實作出兩個 function 來實作加密 & 解密。

輸入、輸出範例

如果輸入 key + 加密 or 解密

如果全部被填滿

其他(全部沒有輸入,key不是數字,沒有輸入key)

提示

ASCII

char --> int 從0~9

char --> int 從a~z & A~Z

變形

// 加密方法
public String 加密(int key, int String) {
    char[] 陣列 = new char[chiper.length()];
    for (int i = 0; i < 陣列.length(); ++i) {
        // 把 String 陣列裡面的值轉成 char
        char c = 陣列.charAt(i);
        // 再把 char 轉成 int 
        int n = (int)c;
        // 判斷 A~Z 其他方法依此類推
        if (n < 91 && n > 64){
        // 開始轉換 n
        n += key - 65;
        // 跟 26 個字母取餘數
        n %= 26;
        // 換回轉好 n
        n += 65;
    }
    //再把int轉回char
    陣列[i]= (char)n;
    //把char轉成String呈現再label上
    label.setText(String.valueOf(陣列));
}

Class JFrame from javax.swing.JFrame

  • public void setTitle(String) Sets the title for this frame to the specified string.
  • public void setSize(int width, int height) Resizes this component(Frame is a kind of Component) so that it has width width and height height.
  • public void setLayout(LayoutManager mgr) Sets the layout manager(ex: GridLayout) for this container.
  • public GridLayout(int rows, int cols) Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.
  • public Component add(Component comp) Appends the specified component to the end of this container.

Class JLabel from javax.swing.JLabel

  • public JLabel(String text) Creates a JLabel instance with the specified text. The label is aligned against the leading edge of its display area, and centered vertically.
  • public void setText(String text) Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.

Class JButton from javax.swing.JButton

  • public JButton(String text) Creates a button with text.
  • public void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this button. Action events occur when a user presses or releases the mouse over this button. If l is null, no exception is thrown and no action is performed.

Class ActionEvent from java.awt.event.ActionEvent

  • public String getActionCommand() Returns the command string associated with this action. This string allows a "modal" component to specify one of several commands, depending on its state. For example, a single button might toggle between "show details" and "hide details". The source object and the event would be the same in each case, but the command string would identify the intended action.

Interface ActionListener from java.awt.event

  • void actionPerformed(ActionEvent e) Invoked when an action occurs.
Modifié le: vendredi 13 mai 2016, 13:54