快速java问题,如何使代码保持沉默
我必须对测试人员运行此代码,并且它需要保持沉默(如果这有意义的话),因为如果它打印出我的计算机将崩溃的所有内容,它将针对大量数据 我不知道如何让它返回静默值快速java问题,如何使代码保持沉默,java,sorting,Java,Sorting,我必须对测试人员运行此代码,并且它需要保持沉默(如果这有意义的话),因为如果它打印出我的计算机将崩溃的所有内容,它将针对大量数据 我不知道如何让它返回静默值 public static void uniqueCharacters(String test){ String temp = ""; for (int i = 0; i < test.length(); i++){ char current = test.charAt(i); if (
public static void uniqueCharacters(String test){
String temp = "";
for (int i = 0; i < test.length(); i++){
char current = test.charAt(i);
if (temp.indexOf(current) < 0){
temp = temp + current;
} else {
temp = temp.replace(String.valueOf(current), "");
}
}
System.out.println(temp + " ");
}
//// here's the tester I'm using
@Test
public void testUniqueCharacters() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int i = 0; i < RUNS; i++) {
int len = rng.nextInt(100) + (2 << rng.nextInt(5));
String s = buildString(rng, len);
String res = P2J2.uniqueCharacters(s);
check.update(res.getBytes());
}
assertEquals(3756363171L, check.getValue());
}
公共静态无效唯一字符(字符串测试){
字符串temp=“”;
对于(int i=0;i让代码正常工作您需要返回您计算的值,而不是打印它:
static String uniqueCharacters(String test){
String temp = "";
//...
return temp;
}
我还想建议另一种方法:读取每个字符,如果它出现在字符串中,则删除所有实例
static String uniqueCharacters(String test) {
String res = test;
for (int i = 0; i < test.length(); i++) {
char current = test.charAt(i);
if (test.indexOf(current, i + 1) >= 0)
res = res.replaceAll(current + "", "");
}
return res;
}
静态字符串唯一字符(字符串测试){
字符串res=测试;
对于(int i=0;i=0)
res=res.replaceAll(当前+“”,“”);
}
返回res;
}
“保持沉默”意味着不打印到控制台?为什么它首先打印它而不返回字符串值?我看不出您的测试人员甚至会进行编译,因为它期望uniqueCharacters
返回一个值,而您显示的uniqueCharacters
被声明为无效。我假设这两个都是same例程。我也不明白你怎么能期望输入随机输入并得到一个预先确定的校验和结果。但这是基于猜测,例如buildString(rng,len)
是。尽管它只是伪随机的,因此是可重复的,但预期的校验和必须来自某个地方。它是手工计算的吗?我的答案是否回答了您的问题?