第1章 Java IO系统 下
作业
编码加密
package ch0320hw; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Encoding { public static void main(String[] args) { PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), true); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入,直到quit结束"); String line = null; char[] ch = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; char[] ch1 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; try { while (!"quit".equals(line = in.readLine())) { StringBuffer sb = new StringBuffer(line); for (int i = 0; i < sb.length(); i++) { for (int j = 0; j < ch.length; j++) { if (sb.charAt(i) == ch[j]) { sb.setCharAt(i, ch[(j + 13) % 26]); break; } if (sb.charAt(i) == ch1[j]) { sb.setCharAt(i, ch1[(j + 13) % 26]); break; } } } System.out.println(sb); } in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("结束................"); } }