如何颠倒短语中的单词顺序和颠倒单词中的字母?

两个问题:

  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);

        }

    }


千巷猫影
浏览 113回答 3
3回答

米脂

编写代码时,始终尝试遵循 KISS 原则。保持简单愚蠢。您迷失在嵌套的 for-if while 循环中,很难找出哪里出了问题。另一个原则是:不要让多个任务使您的方法超载。使用小而简单的方法,一次只执行一项任务。例如,下面我已将reversePhrase和reverseWord放入自己的方法中。这可以帮助您创建一个干净的 main 方法。public static void main(String args[]) {&nbsp; &nbsp; Scanner userInput = new Scanner(System.in);&nbsp; &nbsp; System.out.print("Enter a word or phrase: ");&nbsp; &nbsp; String str = userInput.nextLine();&nbsp; &nbsp; //if input contains spaces call reversePhrase otherwise reverseWord&nbsp; &nbsp; //str.contains(" ") is IMO cleaner, but you can change it to str.indexOf(" ") > -1 if you find it better&nbsp; &nbsp; if(str.contains(" ")){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(reversePhrase(str));&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(reverseWord(str));&nbsp; &nbsp; }}private static String reverseWord(String input) {&nbsp; &nbsp; String result = "";&nbsp; &nbsp; for(int i = input.length()-1; i >= 0; i--){&nbsp; &nbsp; &nbsp; &nbsp; result = result + input.charAt(i);&nbsp; &nbsp; }&nbsp; &nbsp; return result;}private static String reversePhrase(String input) {&nbsp; &nbsp; String result = "";&nbsp; &nbsp; while(input.contains(" ")){&nbsp; &nbsp; &nbsp; &nbsp; result = result + input.substring(input.lastIndexOf(" ")+1) + " ";&nbsp; &nbsp; &nbsp; &nbsp; input = input.substring(0, input.lastIndexOf(" "));&nbsp; &nbsp; }&nbsp; &nbsp; return result + input;}

慕尼黑的夜晚无繁华

在你的 while 循环中:while(position >= 0){&nbsp; &nbsp; //wasn't sure what to put for this parameter&nbsp; &nbsp; &nbsp;reverseLetter = str.charAt(position); // position stays the same&nbsp; &nbsp; &nbsp;reverseWord = reverseLetter + reverseWord;}它不会改变 的值position。(position永远不会是 0)我建议position--在最后添加,如下所示:while(position >= 0){&nbsp; &nbsp; //wasn't sure what to put for this parameter&nbsp; &nbsp; &nbsp;reverseLetter = str.charAt(position); // position stays the same&nbsp; &nbsp; &nbsp;reverseWord = reverseLetter + reverseWord;&nbsp; &nbsp; &nbsp;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) {&nbsp; &nbsp; Scanner userInput = new Scanner(System.in);&nbsp; &nbsp; System.out.print("Enter a word or phrase: ");&nbsp; &nbsp; String str = userInput.nextLine();&nbsp; //user-input string&nbsp; &nbsp; String reversePhrase = "";&nbsp; //placeholder for reversed phrase&nbsp; &nbsp; String reverseWord = "";&nbsp; &nbsp; //placeholder for reversed word&nbsp; &nbsp; char reverseLetter;&nbsp; &nbsp;//placeholder for letters in reversed word&nbsp; &nbsp; final String space = " ";&nbsp; &nbsp; if(str.contains(space)) {&nbsp; &nbsp; &nbsp; &nbsp; while(str.contains(space))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = str.lastIndexOf(space);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String lastWord = str.substring(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str = str.substring(0, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reversePhrase += lastWord;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //We add the first word&nbsp; &nbsp; &nbsp; &nbsp; reversePhrase = reversePhrase + space + str;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(reversePhrase.trim());&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; for(int position = str.length() - 1; position >= 0; position --) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverseLetter = str.charAt(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverseWord =&nbsp; reverseWord + reverseLetter;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(reverseWord);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java