1

爆発しそうです。私はこの問題を解決する方法を2時間探していました。setTimerメソッドにswitchステートメントがあります。プログラムをデバッグすると、メソッドの実行中にtimerType値が変更されますが、メソッドが終了するとすぐに、timerTypeはnullに戻ります。これにより、次回メソッドを呼び出すときに変更する必要があるため、caseステートメントは役に立たなくなります。いつものようにシンプルなものだと確信しているので、あなたの助けが大好きです:(。それが文字列タイプか何かに関係があるかどうかを確認するためにintに変更してみました。ちょっとした初心者です。やめてください私はもう悩むことから(少なくともこの特定の問題について:))

public string timerType;



if (checkRoundChanged()) {
     SoundPlayer.Play();
     setTimer(timerType);
}

と方法

protected void setTimer(String timerType){
        switch (timerType) {
            case "ready":
                secLeft = readySec;
                minLeft = readyMin;
                timerType = "round";
                break;
            case "round":
                secLeft = roundSec;
                minLeft = roundMin;
                timerType = "rest";
                break;
            case "rest":
                secLeft = restSec;
                minLeft = restMin;
                timerType = "round";
                break;
            case "relax":
                secLeft = relaxSec;
                minLeft = relaxMin;
                timerType = "done";
                break;
            default:
                timerType = "ready";
                break;
        }

    }

ありがとうございました!

4

1 に答える 1

4

文字列は、参照ではなく値で渡されます。新しい値を返すことができます。

protected string setTimer(String timerType){
    switch (timerType) {
        case "ready":
            secLeft = readySec;
            minLeft = readyMin;
            timerType = "round";
            break;
        case "round":
            secLeft = roundSec;
            minLeft = roundMin;
            timerType = "rest";
            break;
        case "rest":
            secLeft = restSec;
            minLeft = restMin;
            timerType = "round";
            break;
        case "relax":
            secLeft = relaxSec;
            minLeft = relaxMin;
            timerType = "done";
            break;
        default:
            timerType = "ready";
            break;
    }

    return timerType;
}

....

timerType =  setTimer(timerType);

または参照渡し:

protected void setTimer(ref String timerType) {
   ...
   timerType = newValue;
   ...
}

setTimer(ref timerType);

ここにいくつかの推奨読書があります。

于 2012-10-28T13:38:31.693 に答える