如何反转短语中单词的顺序并反转单词中的字母?



两个问题:

  1. 在用户输入的短语(短语表示两个或多个单词,每个单词之间有一个空格(中,只有最后两个单词被反转,短语中的其他单词不会被反转甚至打印。

  2. 我反转单词中字母顺序的代码(单词表示单个单词(似乎没有打印任何内容,但它无休止地接受输入,但没有结果。

重要说明:我被禁止使用StringBuilder,数组或任何其他"高级"工具。事实上,我最好只使用AP计算机科学指南中引用的方法(尽管不是必需的(。我尝试了很多东西,包括调整参数、不同的串联等。

我希望"这是一个字符串"的输出是"字符串 a 是这个"。相反,它打印"字符串 a"。 我希望"heck"的输出是"kceh"。相反,我一无所获。

注意:代码中的注释是我有的其他关注点和问题,也是为了帮助更好地理解我的代码。对不起,如果它有点混乱,这是我第一次真正评论我自己的代码。

Scanner userInput = new Scanner(System.in);
System.out.print("Enter a word or phrase: ");
String str = userInput.nextLine();  //user-input string 
String reversePhrase = "";  //placeholder for reversed phrase
String reverseWord = "";    //placeholder for reversed word
char reverseLetter = ' ';   //placeholder for letters in reversed word
for(int position = 0; position < str.length(); position++)
{
if(str.indexOf(" ") > -1)   //checks for space in the user-input string
{
while(str.indexOf(" ") > -1)
{
reversePhrase = str.substring(0, str.indexOf(" ")); //this might be the problem, but i'm stuck on any solutions
str = str.substring(1 + str.indexOf(" "));
reversePhrase = str + " "+ reversePhrase;
}
System.out.println(reversePhrase);  //only reverses and prints last two words in phrase
}
else if(!(str.indexOf(" ") > -1))   //if there's no space in the user-input string
{
for(position = str.length() - 1; position >= 0; position --)    //does this conflict with the "for" parameter above?
{
while(position >= 0)    //wasn't sure what to put for this parameter
{
reverseLetter = str.charAt(position);
reverseWord = reverseLetter + reverseWord;
}
}
System.out.println(reverseWord);
}
}

编写代码时,请始终尝试遵循 KISS 原则。保持简单愚蠢。你迷失在嵌套的 for-if-while 循环中,这使得很难弄清楚哪里出了问题。

另一个原则是:不要用多个任务使方法过载。使用一次只执行一项任务的小而简单的方法。例如,下面我将reversePhrasereverseWord放入他们自己的方法中。这有助于您制作一个干净的主方法。

public static void main(String args[]) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter a word or phrase: ");
String str = userInput.nextLine();
//if input contains spaces call reversePhrase otherwise reverseWord
//str.contains(" ") is IMO cleaner, but you can change it to str.indexOf(" ") > -1 if you find it better
if(str.contains(" ")){
System.out.println(reversePhrase(str));
}
else{
System.out.println(reverseWord(str));
}
}
private static String reverseWord(String input) {
String result = "";
for(int i = input.length()-1; i >= 0; i--){
result = result + input.charAt(i);
}
return result;
}
private static String reversePhrase(String input) {
String result = "";
while(input.contains(" ")){
result = result + input.substring(input.lastIndexOf(" ")+1) + " ";
input = input.substring(0, input.lastIndexOf(" "));
}
return result + input;
}

在你的 while 循环中:

while(position >= 0){    //wasn't sure what to put for this parameter
reverseLetter = str.charAt(position); // position stays the same
reverseWord = reverseLetter + reverseWord;
}

它不会更改position的值。(position永远不会为 0(我建议在末尾添加position--,如下所示:

while(position >= 0){    //wasn't sure what to put for this parameter
reverseLetter = str.charAt(position); // position stays the same
reverseWord = reverseLetter + reverseWord;
position--;
}

它将更改position变量的值。

此外,您的代码中有一个if和一个else if。我建议将else if更改为else,因为这毫无意义:

boolean randomBoolean = new java.util.Random().nextBoolean();
if(randomBoolean){...}
else if(!randomBoolean){...} // If randomBoolean == false, then this code will execute anyway

我已经删除了其中一个for循环,因为你不需要它。此外,一个单词大小写的while循环。对于第一种情况,您可以使用另一个字符串来临时保存最后一个单词。结果如下:

public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter a word or phrase: ");
String str = userInput.nextLine();  //user-input string
String reversePhrase = "";  //placeholder for reversed phrase
String reverseWord = "";    //placeholder for reversed word
char reverseLetter;   //placeholder for letters in reversed word
final String space = " ";
if(str.contains(space)) {
while(str.contains(space))
{
int i = str.lastIndexOf(space);
String lastWord = str.substring(i);
str = str.substring(0, i);
reversePhrase += lastWord;
}
//We add the first word
reversePhrase = reversePhrase + space + str;
System.out.println(reversePhrase.trim());
}
else {
for(int position = str.length() - 1; position >= 0; position --) {
reverseLetter = str.charAt(position);
reverseWord =  reverseWord + reverseLetter;
}
System.out.println(reverseWord);
}
}

最新更新