translate.java

来自「nanjing university cs 的java课件。 对新手很有用。付」· Java 代码 · 共 35 行

JAVA
35 行
字号
import java.io.*; 

class Translate {
	public static void main(String[] args) {
		InputStream in = System.in;
		OutputStream out = System.out;
		if (args.length != 2) {
			Translate.error("from / to needed");
		}
		String from = args[0], to = args[1];
		int ch,i;
		if (from.length() != to.length()) {
			error("same length needed");
		}
		try {
			while ( (ch = in.read()) != -1) {
				if ( (i = from.indexOf(ch)) != -1) {
					out.write(to.charAt(i));
				}
				else {
					out.write(ch);
				}
			}
		}
		catch (IOException e) {
			error("I / O Exception : " +e);
		}
	}

	public static void error(String err) {
		System.err.print("Translate :  "+err);
		System.exit(1); //非零值意指“不好”
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?