为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

千巷猫影 2023-11-01 16:36:32
两个问题:在用户输入的短语(表示两个或多个单词之间有空格的短语)中,仅最后两个单词被反转,短语中的其他单词不会被反转甚至打印。我的用于反转单词中字母顺序的代码(单词表示单个单词)似乎没有打印任何内容,但它无休止地接受输入而没有结果。重要提示:我被禁止使用 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);        }    }
查看完整描述

3 回答

?
米脂

TA贡献1836条经验 获得超3个赞

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


另一个原则是:不要让多个任务使您的方法超载。使用小而简单的方法,一次只执行一项任务。例如,下面我已将reversePhrase和reverseWord放入自己的方法中。这可以帮助您创建一个干净的 main 方法。


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;

}


查看完整回答
反对 回复 2023-11-01
?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

在你的 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



查看完整回答
反对 回复 2023-11-01
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

我已经删除了其中一个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);

    }

}


查看完整回答
反对 回复 2023-11-01
  • 3 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信