当它作为junit测试运行时如何修复我的解决方案代码

时间:2015-02-08 06:22:24

标签: java 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显示更改预期为boolean的类型 第二个错误是int actual = s.hasAdjacentRepeats(input)与上述问题相同。

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

1 个答案:

答案 0 :(得分:0)

Java不支持从booleanint的隐式转换(例如,就是这样。因此,应该定义expectedactual作为boolean s,而不是int s。

在不相关的说明中,由于您正在评估s.charAt(i + 1),因此您的循环应该以{{1​​}}结束,而不是s.length() - 1,否则您将获得s.length() 1}}。

相关问题