我怎样才能得到一个单词的第一个字母并将它带到最后?
问问题
1073 次
4 回答
3
你i
每次都取第一个字母,而你应该每次取第一个带索引的字母0
。改变
firstLetter = word.charAt(i);
至
firstLetter = word.charAt(0);
于 2013-12-16T22:05:14.460 回答
1
像这样的东西?
word = "test";
newWord = word.substring(1) + word.substring(0, 1);
于 2013-12-16T22:08:40.240 回答
0
for (int i = 0; i < word.length(); i++) {
firstLetter = word.charAt(0);
word = word.substring(1, word.length());
System.out.println(firstLetter + word);
word += firstLetter;
}
于 2013-12-16T22:13:27.413 回答
0
一种截然不同的方法:保持单词原样,并根据子字符串遍历排列。毕竟:“omputerc”就是[omputer] + [c],就是[c][omputer]交换的;下一个迭代是“mputerco”,它只是 [mputer] + [co] 或 [co][mputer] 交换的,依此类推:
String head, tail;
for (int i = 0, last = word.length()-1; i<last; i++) {
head = word.substring(0,i);
tail = word.substring(i,last);
System.out.println(tail + head);
}
我们将单词保持原样,获取头部和尾部子字符串,并以相反的顺序打印它们,从而准确生成您需要的内容。
于 2013-12-16T23:46:12.820 回答