为什么以下代码未通过测试? [英] Why the following code fails the test ?
问题描述
我发现没有错误和以下代码,代码未通过测试。
我使用JUnit进行测试。有人可以帮帮我吗?
public class BinString {
public BinString(){}
public 字符串 convert( String s){
return 二进制(sum(s));
}
public int sum( String s){
if (s == )
return 0 ;
if (s.length()== 1)
return (( int )(s.charAt( 0 )));
return (( int )(s.charAt( 0 )))+ sum(s.substring( 1 ));
}
public String binarise( int x){
if (x == 0)
return ;
if (x%2 == 1)
return 1 + binarise(x / 2);
return 0 + binarise(X / 2);
}
}
我的尝试:
这是我的考试班
<前lang =java> import static org.junit.Assert。*;
import org.junit.Before;
import org.junit.Test;
public class BinStringTest {
private BinString binString;
@ Before protected void setUp(){
binString = new BinString();
}
@ Test public void testSumFunction(){
int expected = 0;
assertEquals(expected,binString.sum( ));
expected = 100 ;
assertEquals(expected,binString.sum( d));
expected = 256 ;
assertEquals(expected,binString.sum( Add));
}
@ Test public void testBinariseFunction(){
字符串 expected = 101;
assertEquals(预期,binString.binarise( 5 ));
expected = 11111100;
assertEquals(expected,binString.binarise( 252 ));
}
@ Test public void testTotalConversion(){
String expected = 10000001;
assertEquals(expected,binString.convert( A));
}
}
我想你会想要替换这个代码
public String binarise(< span class =code-keyword> int x){
if (x == 0)
return ;
if (x%2 == 1)
return 1 + binarise(x / 2);
return 0 + binarise(X / 2);
}
with
public String 二进制( int x){
if (x == 0)
return ;
if (x%2 == 1)
return binarise(x / 2)+ 1;
return 二进制(x / 2)+ 0\" 跨度>;
}
因为我怀疑那里有一个错误。
I find no error and the following code , the code fails the test.
I used JUnit for testing. Can someone help me ?
public class BinString {
public BinString() {}
public String convert(String s) {
return binarise(sum(s));
}
public int sum(String s){
if(s=="")
return 0;
if(s.length()==1)
return ((int) (s.charAt(0)));
return ((int) (s.charAt(0)))+sum(s.substring(1));
}
public String binarise(int x) {
if(x==0)
return "";
if(x%2==1)
return "1"+binarise(x/2);
return "0"+binarise(x/2);
}
}
What I have tried:
This is my test class
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class BinStringTest {
private BinString binString;
@Before protected void setUp(){
binString = new BinString();
}
@Test public void testSumFunction() {
int expected =0;
assertEquals(expected, binString.sum(""));
expected = 100;
assertEquals(expected, binString.sum("d"));
expected = 256;
assertEquals(expected, binString.sum("Add"));
}
@Test public void testBinariseFunction(){
String expected = "101";
assertEquals(expected, binString.binarise(5));
expected = "11111100";
assertEquals(expected, binString.binarise(252));
}
@Test public void testTotalConversion() {
String expected = "10000001";
assertEquals(expected, binString.convert("A"));
}
}
I think you will want to replace this code
public String binarise(int x) { if(x==0) return ""; if(x%2==1) return "1"+binarise(x/2); return "0"+binarise(x/2); }
with
public String binarise(int x) { if(x==0) return ""; if(x%2==1) return binarise(x/2)+"1"; return binarise(x/2)+"0"; }
as I suspect a bug there.
这篇关于为什么以下代码未通过测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!