我是编写代码的新手,我不是最好的,但我不明白为什么我的代码没有通过我设置的JUnit测试之一。
public class PA3Test {
public static void main(String[] args) {
}
public static int countMajority(int count0, int count1, int count2) {
int allVotes = (count0 + count1 + count2);
int halfVotes = (allVotes / 2);
int winner = 999;
if (count0 >= halfVotes) {
winner = 0;
} else {
winner = -1;
}
if (count1 >= halfVotes) {
winner = 1;
} else {
winner = -1;
}
return winner;
}
测试如下:
import junit.framework.TestCase;
public class PA3TestTest extends TestCase {
public static void testCountMajority() {
assertEquals("0th param should win:", 0,
PA3Test.countMajority(100, 50, 40));
assertEquals("1st param should win:", 1,
PA3Test.countMajority(50, 100, 40));
}
它应该返回0但它返回-1。任何帮助表示赞赏。
答案 0 :(得分:0)
在你的第一次测试中,
allVotes = 190
halfVotes = 95
count0 = 100> 95,获胜者= 0
count1 = 50< 95,获胜者= -1
<小时/> 尝试以下内容,找出你做错了什么。
var targetValue = 'bVal';
var replaceValue = 'yourValue';
str = str.replace(targetValue , replaceValue);
答案 1 :(得分:0)
当你有3个计数时,不确定为什么你将它平均为2。 但根据你的问题陈述,这应该可以解决问题。
public static int countMajority(int count0, int count1, int count2) {
int allVotes = (count0 + count1 + count2);
int halfVotes = (allVotes / 2);
int winner = -1;
if (count0 >= halfVotes) {
winner = 0;
}
if (count1 >= halfVotes && count1 > count0) {
winner = 1;
}
return winner;
}