有没有办法在switch语句中更改大小写?

时间:2019-01-31 01:11:55

标签: javascript switch-statement case onchange stage

因此,我正在处理switch语句,我想知道是否可以针对特定操作更改大小写。

让我说:

var check0 = false;
var check1 = false;

switch(stage){

    case "0":
        rect(10,10,10,10);
        check0 = true;
        break;
    case "1":
        if(check0 === true){
            rect(20,20,20,20);
            check1 = true;
            break;
        }else{
            /* How to set case 0 ? */
            //stage = 0; //Doesnt work
            //stage = "0"; //Doesn't work
        }
        break;
}

4 个答案:

答案 0 :(得分:3)

var check0 = false;
var check1 = false;

function xyz(stage){
    switch(stage){

        case "0":
           rect(10,10,10,10);
           check0 = true;
           break;
        case "1":
           if(check0 === true){
               rect(20,20,20,20);
               check1 = true;
                break;
            }else{
            xyz(0)
            }
          break;
    }
}

答案 1 :(得分:1)

包装功能:

   public bool Open
    {
        get
        {
            return _open;
        }
        set
        {
            _open = value;
            RaisePropertyChanged("Open");
        }
    }

    public RelayCommand OpenButtonClicked
    {
        get;
        private set;
    }

    private bool _open;

    public AppMenuViewModel() : base()
    {
        _open = false;
        OpenButtonClicked = new RelayCommand(() => Open = !Open);
    }

答案 2 :(得分:1)

为此,您可以选择使用@Tarun Khurana和@Artee建议或重组案例的方法,以使case "1"位于case "0"之上,但只有在满足条件时才会中断:< / p>

switch(stage){
    case "1":
        if(check0 === true){
            rect(20,20,20,20);
            check1 = true;
            break;
        } 
    // case "0" is executed since there is no break statement
    case "0":
        rect(10,10,10,10);
        check0 = true;
        break;
}

答案 3 :(得分:1)

尝试一下:

var check0 = false, check1 = false;

function checkSet(stage){
  switch(stage){
    case 0:
      //rect(10,10,10,10);
      check0 = true;
      return;
    case 1:
      if(check0 === true){
        //rect(20,20,20,20);
        check1 = true;
        return;
      }
      else{
        checkSet(0);   
      }
      return;
  }
}

console.log(check0); checkSet(1); console.log(check0);