2017-04-01 98 views
0

我想写一个方法来显示一个游戏的套牌。 我98初始化的Array元素
测试数组是否为空JUNIT

private int [] cards = new int[98]; 

我还创建了一个get和set方法

public int[] getCards() { 
    return cards; 
} 

public void setCards(final int... cards) { 
    this.cards = cards; 
} 

我写的方法称为drawCard。如果我使用这种方法,它应该从Array中删除第一个元素并将其返回。

public int drawCard() throws IndexOutOfBoundsException { 
    if (getCards().length == 0) { 
     throw new IndexOutOfBoundsException("No Cards Left!"); 
    } 
    setCards(ArrayUtils.removeElement(getCards(), 0)); 
    return getCards()[0]; 
} 

然后我写了JUnit测试。 测试应删除所有98个元素,然后数组应为空== 0。 但测试总是停在1

@Test 
public void testDrawCard() { 
    Deck deck = new Deck(); 
    assertThat(deck.getCards().length).isEqualTo(98); 

    for(int x = 98; x >= 0; x--){ 
     deck.drawCard(); 
    } 
    assertThat(deck.getCards().length).isEqualTo(0); 
} 

错误消息:

java.lang.ArrayIndexOutOfBoundsException: 0 
at edu.hm.hafner.java2.thegame.Deck.drawCard(Deck.java:35) 

35 =返回行

at edu.hm.hafner.java2.thegame.DeckTest.testDrawCard(DeckTest.java:40) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 

是否有人知道什么是错?

+0

你从98循环到!包容性! 0. – Androbin

+0

您正试图删除99个元素。 – Androbin

+0

只需将> =更改为简单的> – Androbin

回答

0

@ajb是正确的,但是当你想正常向后遍历,你开始长度 - 1因为指数0开始所以在你的风格,环路应该

for(int x = 98 - 1; x >= 0; x--) 

但是,无论,你并没有在循环中使用x变量,所以只要你绘制了98个元素,顺序就不重要。

1
for(int x = 98; x >= 0; x--) 

这执行循环,而x >= 0。也就是说,如果我们递减x并且它的值为0,我们再次执行循环,因为0 >= 0为真。然后,我们递减x并发现它是-1,所以循环停止。这意味着循环将执行x = 98,97,96,...,2,1,0。此列表中有99个数字,所以循环执行99次。

由于您根本没有使用x,所以没有理由让它从顶部开始往下走。如果唯一的目的就是要确保你的循环执行恰好98次,那么就不要弄巧 - 只需使用标准for环成语:

for (int i = 0; i < 98; i++) 

它不会对程序的执行差异。但通过坚持标准习惯用法,除非必要,您将节省一些脑细胞,并最终得到更少的错误。

+0

谢谢你的回答。 但是,如果我改变循环到 '我= 0,我<98,我++'我仍然得到相同的异常 错误消息总是告诉我有返回语句有问题,但我不明白 – Konstantin

+0

错误消息是否字面意思地表示“返回语句有问题”?还是提供更多信息?请不要隐藏我们的重要信息。即使你不明白它的意思,我们其他人也会。所以不要保守我们的秘密。 – ajb

+0

所以我刚刚编辑了测试方法 '@Test public void testDrawCard(){ Deck deck = new Deck(); assertThat(deck.getCards().length).isEqualTo(98);对于(int i = 0; i <98; i ++){ deck.drawCard(); } assertThat(deck.getCards().length).isEqualTo(0); }' 但错误依然是 'java.lang.ArrayIndexOutOfBoundsException:0 \t在edu.hm.hafner.java2.thegame.Deck.drawCard(Deck.java:35) \t在EDU。 hm.hafner.java2.thegame.DeckTest.testDrawCard(DeckTest.java:40)' – Konstantin