题目描述
A common typing error is to place the hands on the keyboard one row to the right of the correct position. So "Q" is typed as "W" and "J" is typed as "K" and so on. You are to decode a message typed in this manner.
输入描述:
Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.
输出描述:
You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.
示例1
输入
复制
O S, GOMR YPFSU/
输出
复制
I AM FINE TODAY.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Main
{
public static void main(String[] args){
try {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
HashMap<Character, Character> map = new HashMap<>();
map.put('W', 'Q');
map.put('E', 'W');
map.put('R', 'E');
map.put('T', 'R');
map.put('Y', 'T');
map.put('U', 'Y');
map.put('I', 'U');
map.put('O', 'I');
map.put('P', 'O');
map.put('[', 'P');
map.put(']', '[');
map.put('\\', ']');
map.put('S', 'A');
map.put('D', 'S');
map.put('F', 'D');
map.put('G', 'F');
map.put('H', 'G');
map.put('J', 'H');
map.put('K', 'J');
map.put('L', 'K');
map.put(';', 'L');
map.put('\'', ';');
map.put('X', 'Z');
map.put('C', 'X');
map.put('V', 'C');
map.put('B', 'V');
map.put('N', 'B');
map.put('M', 'N');
map.put(',', 'M');
map.put('.', ',');
map.put('/', '.');
map.put('1', '`');
map.put('2', '1');
map.put('3', '2');
map.put('4', '3');
map.put('5', '4');
map.put('6', '5');
map.put('7', '6');
map.put('8', '7');
map.put('9', '8');
map.put('0', '9');
map.put('-', '0');
map.put('=', '-');
String[] parts = str.split(" ");
char[] ch0 = parts[0].toCharArray();
for(int j = 0; j < ch0.length; j++)
ch0[j] = map.get(ch0[j]);
System.out.print(new String(ch0));
for(int i = 1; i < parts.length; i++) {
char[] ch = parts[i].toCharArray();
for(int j = 0; j < ch.length; j++)
ch[j] = map.get(ch[j]);
System.out.print(" "+new String(ch));
}
System.out.print("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
来源:CSDN
作者:东山阿强
链接:https://blog.csdn.net/weixin_43306331/article/details/104174090