【问题标题】:Reversing a sentence in java在java中反转句子
【发布时间】:2018-03-16 12:55:00
【问题描述】:

我正在挑战在https://www.codewars.com/kata/reversed-words/train/java 反转给定句子,我已经设法将句子反转为预期但在他们的 JUnit 测试中遇到了一个小错误。 这是我将任何句子反转为预期结果的代码,例如

“最大的胜利是不需要战斗的胜利”
// 应该返回 "battle no requires which is the 胜利最大"

我的代码

    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

【问题讨论】:

  • 循环结束后删除结尾空格字符。命名以大写字符开头的变量不遵守 Java 命名约定
  • @sarkasronie 对不起,我的错。
  • 具体在哪里,我应该在哪里删除结束空格字符

标签: java


【解决方案1】:

试试这个方法

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(),您可以省去麻烦,避免将分隔符添加到最后一个元素。

【讨论】:

  • 它没有工作,兄弟,我仍然得到以前的错误
  • 这没有意义。上面的示例确实有效。请检查是否有任何输入错误。
  • 没有理由将String[] 分配给split()。当然可以,但这不是一种干净的方法。
  • @KarolDowbecki 我不知道如何以更有效的方式解决这个问题。
【解决方案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

【讨论】:

  • 嘿@Karol Assert.assertEquals() 不是那个提出这个挑战的人......我如何确保最后一个词没有添加“”?
  • 如果你想破解,请使用return ExpectedSentence.trim()。更好的解决方案是使用StringBuilder 并跟踪正在处理的空间。
猜你喜欢
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多