在java中反转一个句子

时间:2018-03-16 12:55:01

标签: java

我正在做https://www.codewars.com/kata/reversed-words/train/java翻转给定句子的挑战,我已经设法将句子反转到预期,但在他们的JUnit测试中出现轻微错误。 这是我的代码,可以将任何句子转换为预期的结果,例如

  

“最大的胜利就是不需要战斗的胜利”   //应该回归“战斗没有要求哪个是最大的胜利”

我的代码

    public class ReverseWords{
 public static String reverseWords(String sentence){
   String reversedsentence ="";

       for(int x=sentence.length()-1;x>=0;--x){  //Reversing the whole sentence                                                           

            reversedsentence += sentence.charAt(x);                       
       } //now you are assured the whole sentence is reversed

       String[]words = reversedsentence.split(" "); //getting each word in the  reversed sentence and storing it in a string array

       String ExpectedSentence= "";
        for(int y=0;y<words.length;y++){
            String word =words[y];   //getting word by word in the  string array

            String reverseWord = "";
            for(int j=word.length()-1;j>=0;j--){    /*Reversing each word  */

                 reverseWord += word.charAt(j);    
            }
            ExpectedSentence +=reverseWord + " ";   //adding up the words to get the expected sentence

        }

            return ExpectedSentence;
    }
}

并且有JUnit测试代码

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

// TODO: Replace examples and use TDD development by writing your own tests

public class SolutionTest {
    @Test
    public void testSomething() {
         assertEquals(ReverseWords.reverseWords("I like eating"), "eating like I");
         assertEquals(ReverseWords.reverseWords("I like flying"), "flying like I");
         assertEquals(ReverseWords.reverseWords("The world is nice"), "nice is world The");
    }
}

错误到了

> expected:<eating like I[ ]> but was:<eating like I[]> 

有关错误的更多详细信息

> org.junit.ComparisonFailure:  expected:<eating like I[ ]> but was:<eating like I[]>   at org.junit.Assert.assertEquals(Assert.java:115)   at org.junit.Assert.assertEquals(Assert.java:144)   at SolutionTest.testSomething(SolutionTest.java:10)

您只需点击此链接并粘贴我的代码即可看到代码播放地Train: Reversed Words |CodeWars

2 个答案:

答案 0 :(得分:3)

以这种方式试试

public static void main(String[] args) {
    Assert.assertEquals("sentence a is This", reverseSentence("This is a sentence"));
}


public static String reverseSentence(String sentence) {
    String[] words = sentence.split(" ");
    for (int i = 0; i < words.length / 2; i++) {
        String temp = words[i];
        words[i] = words[words.length - i - 1];
        words[words.length - i - 1] = temp;
    }
    return String.join(" ", words);
}

使用String.join(),您可以省去麻烦,以避免将分隔符添加到最后一个元素。

答案 1 :(得分:2)

首先,您错误地使用Assert.assertEquals(),因为应首先提供expected参数。将其更改为:

assertEquals("eating like I", ReverseWords.reverseWords("I like eating"));

使错误清晰:

> expected:<eating like I[]> but was:<eating like I[ ]> 

这是由以下行引起的,该行在每个reverseWord处理后盲目地添加空格:

ExpectedSentence +=reverseWord + " ";   //adding up the words to get the expected sentence