UVA 10082 WERTYU
一、问题描述
敲击键盘时,稍不注意就会右错一位。
比如想按Q的时候,按成了W。
假设输入时,每一位都右错一位。
给出敲错的结果,求原本正确的输入。
二、输入格式
多行字符串
三、输出格式
多行字符串(对应输入)
四、样例输入
O S, GOMR YPFSU/
五、样例输出
I AM FINE TODAY.
六、思路
构建常量字符串(包含所有需要更改的字符):
String table = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
使用StringBuilder重构字符串:
StringBuilder sb = new StringBuilder();
对每个输入字符,如果常量字符串中包含该字符,向sb中添加该字符左侧的字符,
否则,直接向sb中添加该字符。
七、代码
import java.util.*;
import java.lang.Math.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String table = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
while(scan.hasNextLine()) {
String s = scan.nextLine();
StringBuffer sb1 = new StringBuffer();
for(int i=0; i<s.length(); i++) {
if(table.indexOf(s.charAt(i))!=-1) {
sb1.append(table.charAt(table.indexOf(s.charAt(i))-1));
}else {
sb1.append(s.charAt(i));
}
}
System.out.println(sb1.toString());
}
}
}
八、总结
- 常量字符串的使用