我即将爆炸。我一直在寻找两个小时来解决这个问题。我在setTimer方法中有一个switch语句。当我调试程序时,timerType值在方法运行时发生变化,但是一旦退出,timerType将恢复为null。这使我的case语句变得毫无用处,因为我需要在下次调用方法时进行更改。我很乐意帮助你,因为我确信它像往常一样简单:(。我尝试将它更改为int以查看它是否与String类型或其他任何事情有关。我有点像菜鸟。请停止我不再挣扎(至少在这个特殊的问题:) :)
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;
}
}
三江源!
答案 0 :(得分: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);
以下是一些recommended reading。