String word = "ABCD";
StringBuffer str = new StringBuffer (word);
int counter = 0;
for (int ch = 0; ch < word.length(); ch ++)
{
int number = word.charAt(ch)- 'A' + 1;
str.setCharAt(counter, (char) number);
if (ch != word.length ()-1)
str.insert(counter +1, '-');
counter += 2;
}
System.out.println (str);
}我希望我的输出是1-2-3-4,所以A=1和B= 2.等等,我们可以假设所有的输入都是大写的。但我的代码会产生随机符号。那么,如何在不重新编写整个程序的情况下修复代码以生成1-2-3-4呢?
发布于 2014-02-22 01:27:45
您只缺少了从int返回到char的转换。线
str.setCharAt(counter, (char) number);应该是
str.setCharAt(counter, (char) ('0' + number));https://stackoverflow.com/questions/21948341
复制相似问题