switch语句出错等问题

hc2pp10m  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(293)

每当使用switch语句调用此方法时,我都会遇到两个问题:
有时(是的,只是有时,不是每次)当变量 mDNumHit 值为1时,运行的是案例2或案例3,而不是案例1。
什么时候 mDNumhit 是1,它运行案例1。现在,假设 dCards3 是9,案例9已经运行,但只有这部分代码

new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs9);
                                        }
                                    }, 500);

是运行的,这个

dTotal += 9;
dTotalAlt += 9;

没有(变量没有更新)。
为什么会这样?我只是不知道怎么了。
代码:

public boolean dealerHit() {

        final AnimationSet as = new AnimationSet(true);
        as.addAnimation(animTranslate);
        as.addAnimation(animSet);

        final Random r = new Random();

        //dealer hit
        while ((dTotal < 17) && (dTotalAlt < 17) && ((mDNumHit + 2) < mMaxCards)) {

            //update num of hit by dealer
            mDNumHit++;

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    cardSound.start();

                    switch (mDNumHit) {
                        case 1:
                            //create a random card
                            dCard3 = r.nextInt(52) + 1;
                            //dHitTemp = dCard3;

                            mImgDCard3.setVisibility(View.VISIBLE);
                            mImgDCard3.startAnimation(as);

                            switch (dCard3) {
                                case 1:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs_a);
                                        }
                                    }, 500);
                                    isDAce3 = true;
                                    if (isDAce1 && isDAce2) {

                                        dTotal = 3;
                                        dTotalAlt = 13;

                                    } else if (isDAce1 && !isDAce2) {

                                        dTotalAlt = dTotal + 11;
                                        dTotal += 1;

                                    } else if (!isDAce1 && isDAce2) {

                                        dTotalAlt = dTotal + 11;
                                        dTotal += 1;

                                    } else {

                                        dTotal += 1;
                                        dTotalAlt += 11;

                                    }
                                    break;
                                case 2:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs2);
                                        }
                                    }, 500);
                                    dTotal += 2;
                                    dTotalAlt += 2;
                                    break;
                                case 3:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs3);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs3);
                                    dTotal += 3;
                                    dTotalAlt += 3;
                                    break;
                                case 4:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs4);
                                        }
                                    }, 500);
                                    dTotal += 4;
                                    dTotalAlt += 4;
                                    break;
                                case 5:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs5);
                                        }
                                    }, 500);
                                    dTotal += 5;
                                    dTotalAlt += 5;
                                    break;
                                case 6:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs6);
                                        }
                                    }, 500);
                                    dTotal += 6;
                                    dTotalAlt += 6;
                                    break;
                                case 7:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs7);
                                        }
                                    }, 500);
                                    dTotal += 7;
                                    dTotalAlt += 7;
                                    break;
                                case 8:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs8);
                                        }
                                    }, 500);
                                    dTotal += 8;
                                    dTotalAlt += 8;
                                    break;
                                case 9:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs9);
                                        }
                                    }, 500);
                                    dTotal += 9;
                                    dTotalAlt += 9;
                                    break;
                                case 10:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs10);
                                        }
                                    }, 500);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                                case 11:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.clubs_j);
                                        }
                                    }, 500);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;

...other cases...

                                case 51:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.spades_q);
                                        }
                                    }, 500);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                                case 52:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard3.setImageResource(R.drawable.spades_k);
                                        }
                                    }, 500);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                            }
                            break;
                        case 2:
                            //create a random card
                            dCard4 = r.nextInt(52) + 1;
                            //dHitTemp = dCard4;

                            mImgDCard4.setVisibility(View.VISIBLE);
                            mImgDCard4.startAnimation(as);

                            switch (dCard4) {
                                case 1:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs_a);
                                        }
                                    }, 500);
                                    isDAce4 = true;
                                    if ((isDAce1 && isDAce2) && isDAce3) {

                                        dTotal = 4;
                                        dTotalAlt = 14;

                                    } else if (((isDAce1 && isDAce2) && !isDAce3) || ((isDAce1 && !isDAce2) && isDAce3) || ((!isDAce1 && isDAce2) && isDAce3)) {

                                        dTotalAlt = dTotal + 11;
                                        dTotal += 1;

                                    } else if (((isDAce1 && !isDAce2) && !isDAce3) || ((!isDAce1 && isDAce2) && !isDAce3) || ((!isDAce1 && !isDAce2) && isDAce3)) {

                                        dTotalAlt = dTotal + 11;
                                        dTotal += 1;

                                    } else {

                                        dTotal += 1;
                                        dTotalAlt += 11;

                                    }
                                    break;
                                case 2:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs2);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs2);
                                    dTotal += 2;
                                    dTotalAlt += 2;
                                    break;
                                case 3:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs3);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs3);
                                    dTotal += 3;
                                    dTotalAlt += 3;
                                    break;
                                case 4:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs4);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs4);
                                    dTotal += 4;
                                    dTotalAlt += 4;
                                    break;
                                case 5:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs5);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs5);
                                    dTotal += 5;
                                    dTotalAlt += 5;
                                    break;
                                case 6:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs6);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs6);
                                    dTotal += 6;
                                    dTotalAlt += 6;
                                    break;
                                case 7:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs7);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs7);
                                    dTotal += 7;
                                    dTotalAlt += 7;
                                    break;
                                case 8:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs8);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs8);
                                    dTotal += 8;
                                    dTotalAlt += 8;
                                    break;
                                case 9:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs9);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs9);
                                    dTotal += 9;
                                    dTotalAlt += 9;
                                    break;
                                case 10:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs10);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs10);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                                case 11:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.clubs_j);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs_j);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;

                               ....other cases...

                                case 51:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.spades_q);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.spades_q);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                                case 52:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard4.setImageResource(R.drawable.spades_k);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.spades_k);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                            }
                            break;
                        case 3:
                            //create a random card
                            dCard5 = r.nextInt(52) + 1;
                            //dHitTemp = dCard5;

                            mImgDCard5.setVisibility(View.VISIBLE);
                            mImgDCard5.startAnimation(as);

                            switch (dCard5) {
                                case 1:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs_a);
                                        }
                                    }, 500);
                                    isDAce5 = true;
                                    if ((isDAce1 && isDAce2) && (isDAce3 && isDAce4)) {

                                        dTotal = 5;
                                        dTotalAlt = 15;

                                    } else if ((!isDAce1 && !isDAce2) && (!isDAce3 && !isDAce4)) {

                                        dTotal += 1;
                                        dTotalAlt += 11;

                                    } else {

                                        dTotalAlt = dTotal + 11;
                                        dTotal += 1;

                                    }
                                    break;
                                case 2:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs2);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs2);
                                    dTotal += 2;
                                    dTotalAlt += 2;
                                    break;
                                case 3:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs3);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs3);
                                    dTotal += 3;
                                    dTotalAlt += 3;
                                    break;
                                case 4:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs4);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs4);
                                    dTotal += 4;
                                    dTotalAlt += 4;
                                    break;
                                case 5:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs5);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs5);
                                    dTotal += 5;
                                    dTotalAlt += 5;
                                    break;
                                case 6:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs6);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs6);
                                    dTotal += 6;
                                    dTotalAlt += 6;
                                    break;
                                case 7:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs7);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs7);
                                    dTotal += 7;
                                    dTotalAlt += 7;
                                    break;
                                case 8:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs8);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs8);
                                    dTotal += 8;
                                    dTotalAlt += 8;
                                    break;
                                case 9:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs9);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs9);
                                    dTotal += 9;
                                    dTotalAlt += 9;
                                    break;
                                case 10:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs10);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs10);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                                case 11:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.clubs_j);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.clubs_j);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;

                                ....other cases.....

                                case 52:
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImgDCard5.setImageResource(R.drawable.spades_k);
                                        }
                                    }, 500);
                                    //img.setImageResource(R.drawable.spades_k);
                                    dTotal += 10;
                                    dTotalAlt += 10;
                                    break;
                            }
                            break;
                    }

                    //check if dealer burst or not (when hitting)
                    if (dTotal > 21) {//dealer burst

                        displayWin();

                        isBurst = true;
                        return; //exit from delay function

                    }

                    //flag dealer hit max card
                    if ((mDNumHit + 2) == mMaxCards) {

                        isDealerMax = true;

                    } else {

                        isDealerMax = false;

                    }

                }
            }, 1500); //end delay function

        } //end while loop

        if (isBurst) {

            return true;

        } else {

            return false;
        }

    }
knpiaxh1

knpiaxh11#

mDNumHit 是一个字段,但是,您正在创建一个具有runnable的处理程序—它强烈建议多个线程。
所以,发生的事情是,一个线程在切换之前改变了mdnumhit。修复方法是不为此使用字段,而是将mdnumhit的值传递给处理程序。
这有点像有一个全球性的白板,每次有一个新的销售,有人跑上来,抹去数字,并写下下下一个数字。
想象一下,那些将订单打包到一个盒子里的人,每次都要查看白板,然后再查看订单。如果有人在 Package 过程中更改了号码,这将是完全错误的。
解决办法是告诉他们根本不要看白板——把需要打包的订单号递给他们,告诉他们只看那个。

相关问题