2017-04-01 82 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