2015-02-08 40 views
-4

这里是没有问题如何解决我的解决方案的代码,当它运行作为JUnit测试

public boolean hasAdjacentRepeats(String s){ 

     for(int i = 0; i<s.length(); i++){ 

       if(s.charAt(i) == s.charAt(i + 1)){ 
        return true; 


       } 


     } 
     return false; 
    } 

我的解决方案的代码,但在我的解决方案的代码它像

@Test public void tests6(){ 
    code.Solution s = new code.Solution(); 
    String input = "hhhhhey "; 
    int expected = true; 
    int actual = s.hasAdjacentRepeats(input); 
    assertTrue("Expected was" +true+"but the actual was" +false , expected == actual); 

}

第一个错误是真实的。 eclipse显示预期为布尔型的更改类型 第二个错误是int actual = s.hasAdjacentRepeats(input)与上述问题相同。

所以我不知道修复我的解决方案代码的适当方式是什么。

+3

你知道什么是'int'意思? – immibis 2015-02-08 06:23:30

回答

0

Java不支持隐式转换从booleanint(像,例如,一样。因此,无论是expected因为你actual应定义为boolean S,不int秒。

在一个不相关的音符, “重新评估s.charAt(i + 1),你的循环应该结束在s.length() - 1,不s.length(),否则你会得到一个IndexOutOfBoundsException

+0

谢谢你这么多,我完全解决我的问题! – sunqifeng 2015-02-09 04:06:54