Eclipse - 老虎机,if和else如果不正确地增加int

时间:2012-03-18 02:18:16

标签: java

我正在创造一个带摆动的老虎机。以下代码必须检查并分配结果:

// 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 [0],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,而是因为硬币只是在没有任何明显原因的情况下更改为更大的数字..

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

也许你正在调试,你在观察区域的“表情”上有一些修改句子? 如果发生这种情况,它们会在代码执行之前或之后在分离的线程中随机进行评估,因此结果很奇怪