我试图为这个问题编写解决方案:http://sharecode.io/section/problemset/problem/1088但我收到运行时错误 和Eclipse kepler和ideone.com编译对我来说是不同的。我不明白为什么。
实际上,我想问为什么这个代码System.out.println("String #"+(i+1));
首先在for循环中运行,而str = br.readLine();?根本没有正常工作?
import java.io.IOException;
public class Main {
public static void main(String args[]) throws IOException {
java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
java.io.BufferedReader br = new java.io.BufferedReader(isr, 16 * 1024);
int testcase = br.read();
String str = "";
String[] strarray=null;
for(int i=0;i<testcase;i++){
str = br.readLine();
strarray = str.split("");
for (int j = 1; j < strarray.length; j++) {
if (strarray[j].equals("A"))
strarray[j] = "B";
else if (strarray[j].equals("B"))
strarray[j] = "C";
else if (strarray[j].equals("C"))
strarray[j] = "D";
else if (strarray[j].equals("D"))
strarray[j] = "E";
else if (strarray[j].equals("E"))
strarray[j] = "F";
else if (strarray[j].equals("F"))
strarray[j] = "G";
else if (strarray[j].equals("G"))
strarray[j] = "H";
else if (strarray[j].equals("H"))
strarray[j] = "I";
else if (strarray[j].equals("I"))
strarray[j] = "J";
else if (strarray[j].equals("J"))
strarray[j] = "K";
else if (strarray[j].equals("K"))
strarray[j] = "L";
else if (strarray[j].equals("L"))
strarray[j] = "M";
else if (strarray[j].equals("M"))
strarray[j] = "N";
else if (strarray[j].equals("N"))
strarray[j] = "O";
else if (strarray[j].equals("O"))
strarray[j] = "P";
else if (strarray[j].equals("P"))
strarray[j] = "Q";
else if (strarray[j].equals("Q"))
strarray[j] = "R";
else if (strarray[j].equals("R"))
strarray[j] = "S";
else if (strarray[j].equals("S"))
strarray[j] = "T";
else if (strarray[j].equals("T"))
strarray[j] = "U";
else if (strarray[j].equals("U"))
strarray[j] = "V";
else if (strarray[j].equals("V"))
strarray[j] = "W";
else if (strarray[j].equals("W"))
strarray[j] = "X";
else if (strarray[j].equals("X"))
strarray[j] = "Y";
else if (strarray[j].equals("Y"))
strarray[j] = "Z";
else if (strarray[j].equals("Z"))
strarray[j] = "A";
}
System.out.println("String #"+(i+1));
for(int k =0 ; k<strarray.length;k++)
System.out.print(strarray[k]);
}
}
}
答案 0 :(得分:0)
使用
int testcase = br.read();
您从输入中读取第一个字符。如果你的整个输入只有一个字符, 已读取整行,以便
str = br.readLine();
没有任何内容,您看到的第一个输出是:String #1
您可以将测试用例编号更改为:
int testcase = Integer.valueOf(br.readLine());
但你应该处理NumberFormatException
答案 1 :(得分:0)
你在这里犯了错误:
int testcase = br.read();
该行只读取一个字节,但字的实数是一个字符串。
如果输入为“24”(后跟行尾字符),br.read()
将只返回字节表示“2”,即50,那么你的循环太长了以下br.readLine();
1}}将返回“4”(“2”后第1行的剩余字符)
所以你应该用
替换int testcase = br.read();
int testcase = Integer.parseInt(br.readLine());