...并将它们放入大小为4的数组中。
索引0 =多少个大写字母
索引1 =多少个小写字母
索引2 =多少个数字
索引3 =其他
这是我的代码:
package code;
public class PotentialQ {
public int[] countAll(String input){
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int [] fin = new int [4];
for(int i = 0; i < input.length(); i++){
char ch = input.charAt(i);
if(Character.isLetter(ch)){
if(Character.isUpperCase(ch)){
count1++;
}
else
count2++;
}
else if(Character.isDigit(ch)){
count3++;
}
else
count4++;
}
fin[0] = count1;
fin[1] = count2;
fin[2] = count3;
fin[3] = count4;
return fin;
}
}
这是我的测试:
package tests;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class PotentialQTests {
int [] arr = {1,3,3,1};
@Test public void test1() {testCode("Baaa123!", arr);}
private void testCode(String input, int [] expected) {
code.PotentialQ pq = new code.PotentialQ();
int [] actual = pq.countAll(input);
assertTrue("It was " +actual, expected == actual);
}
}
我的错误是一堆字母和数字。 我的代码有什么问题???
答案 0 :(得分:0)
在这一行:
assertTrue("It was " +actual, expected == actual);
你在原始数组上调用==,我建议将其更新为
assertArrayEquals(expected, actual);
描述“它是”+实际不会告诉你太多 - 只是阵列的参考。要查看您可以执行的内容
assertArrayEquals("It was " + Arrays.toString(actual), expected, actual);