Java 入門指南 V2.00 - 單元 15 範例




Encrypt03.java


public class Encrypt03 {
   // 密碼表字元陣列
   private char[] code = new char[26];

   // 建構子     
   public Encrypt03() {
      setCode();
   }

   // setter
   public void setCode() {
      int a = 0;
      int b = 0;

      a = (int) (Math.random() * 10);
      b = (int) (Math.random() * 10);

      int x, y, m, i;
      char c = 'a';
      for (i = 0; i < 26; i++) {
         x = c;
         y = x * a + b;
         m = y % 26;
         code[i] = (char) (m + 97);
         c++;
      }
   }

   // getter
   public char[] getCode() {
      return code;
   }

   // 編碼的方法
   public String toEncode(String s) {
      return s;
   }

   // 解碼的方法
   public String toDecode(String s) {
      return s;       
   }

   // 測試的 main()
   public static void main(String[] args) {
      for (int i = 0; i < 16; i++) {
         Encrypt03 e = new Encrypt03();
         System.out.println(e.getCode());
      }
      //String s = "There is no spoon";
      //System.out.println(s);
      //String s1 = e.toEncode(s);
      //System.out.println(s1);
      //String s2 = e.toDecode(s1);
      //System.out.println(s2);
   }
}
 
/* 檔名: Encrypt03.java
   作者: Kaiching Chang
   時間: September, 2014  */

the end

沒有留言: