2012-03-18 3 views
0

私はスイングでスロットマシンを作成しています。 [0]、NUMEclipse - スロットマシン、ifとelseがintを間違って増やす場合

// Reset win amount 
    winAmount = 0; 

    // Checks for winning combinations and increases corresponding values 
    if (num[0]==num[1] && num[1]==num[2]){ 
     winAmount = 4; 
     coinsWon = coinsWon + winAmount; 
     txtWinMessage.setText("Jackpot! You have won " + winAmount + " !"); 
     txtCoinsWon.setText(coinsWon + " "); 

     // A text field for testing - so i can see the math it does 
     textField_6.setText(coinsWon + " " + winAmount + "/0 = 1"); 
    } else if (num[0]==num[1] || num[0]==num[2] || num[1]==num[2]){ 
     winAmount = 1; 
     coinsWon = coinsWon + winAmount; 
     txtWinMessage.setText("You Have Won " + winAmount); 
     txtCoinsWon.setText(coinsWon + " "); 

     // A text field for testing - so i can see the math it does 
     textField_6.setText(coinsWon + " " + winAmount + "/0 = 1"); 
    } else { 
     txtWinMessage.setText("Sorry, try again"); 

     // A text field for testing - so i can see the math it does 
     textField_6.setText("Went to else. coinsWon = " + coinsWon); 
    } 

NUMを[1]とNUM [2]このビットを介して生成される:以下のコードは結果をチェックし、割り当てる有する

private void setIcon(int i) { 
    num[i] = random.nextInt(8); 
    slots[i].setIcon(new ImageIcon(getClass().getResource("images/slotIcon" + num[i] + ".png"))); 
    counter++; 
    if(counter > 50) { 
     winAmount = 0; 
     getResults(); 
    } 
} 

私は試験フィールドがnum [i]が正しく割り当てられ、ImageIconsがnum [i]に従って正しく設定されていることを示してください。結果の計算には問題が発生しますが、 IF部に行くと、コインに+4を加えます。もし、ELSEに行くなら、コインに+1を加えれば、それはELSEに行くと時には何も追加しませんが、 1をコインに、または+2をコインにする。しかし、winAmountが1になるのではなく、coinsWonが明らかな理由なしに大きな数値に変化するだけであるからです。

アイデア?

+2

最後の段落を再フレーズできますか? –

+0

基本的には、else {セクションに行くとcoinsWonが増えます。 – Lynce

+4

私はそれをデバッグすることをお勧めします。 –

答えて

0

多分あなたはデバッグをしていて、 "式"のウォッチエリアにいくつかの文章がありますか? コード実行前または実行後にコードがランダムに分割されたスレッドで評価されているため、結果が奇数です

関連する問題