繁体   English   中英

有没有办法简化这个嵌套的 if-else 语句?

[英]Is there a way to simplify this nested if-else statements?

我有这个伪代码,它都播放不同的音频文件。 我读到嵌套 if 的时间复杂度为 O(m+n),我认为这还可以,但如果有一种方法可以简化它或降低时间复杂度,那就太好了。

btn_G.onTouchListener(){
  Case Motion.Action_Down:
    if(high_octave){ 

      if(move){ //if the phone is moving
      
        if(sharp){ //if it's a sharp note
        
          if(vibrato){ //if it's vibrato
            Gstream = G.play(loop) //loop a vibrato, sharp, high octave G note

          } else { //if there is no vibrato
            Gstream = G.play(loop) //loop a normal, sharp, high octave G note
          }
          
        } else { //if all are normal notes, no sharps
        
          if(vibrato){ //if there is vibrato
            Gstream = G.play(loop) //loop a vibrato, normal, high octave G note

          } else { //if there is no vibrato
            Gstream = G.play(loop) //loop a normal, high octave G note
          }
          
        }
      } else { //if the phone is not moving
      //if the phone doesn't move, doesn't matter if there is vibrato or not
      
        if(sharp){ //if it's a sharp note
          Gstream = G.play(once) //play a sharp, high octave G note only once

        } else { //if all are normal notes, no sharps
          Gstream = G.play(once) //play a normal, high octave G note only once
        }
        
      }
    } else if(mid_oct){ #middle octave
        //repeat all of that but with middle octave G note

    } else { #low octave
        //repeat all of that but with low octave G note
    }
  Case Motion.Action_Up:
    Gstream = G.stop() // stop the audio
}

那只是一个按钮。 我有 8 个按钮需要这样做。 我想过使用哈希列表,但为了创建列表,我也会检查这样的条件,所以它会是一样的,不是吗?

我也发现了这个,但我不是像嵌套 if 那样分离条件吗? 因为我有 8 个按钮,所以它也会很长且重复?

即使有所有条件,有没有办法缩短它?

抱歉,我对此有点陌生。

您可以为条件使用位掩码,并将它们分组以用于switch语句:

int cond1 = 0x1;
int cond2 = 0x2;

switch(cond) {
  case cond1|cond2:
    // both  are true
    break;
  case cond1|0x0:
    // only cond1 true
    break;
  case 0x0|cond2:
    // only cond2 true
    break;
  case 0x0|0x0:
    // both are false
    break;
}

我最终将它们分成不同的方法,如下所示:

G_btn.onTouch{
  Case Motion Down:
    if(up){
      horizontal_method(G5, G#5, gStream)
    } else if (down) {
      horizontal_method(G4, G#4, gStream)
    }
  Case Motion Up:
    stop_audio_method(gStream)
}

public void horizontal_method(note, sharp, stream){
  if(horizontal){ //if phone is moving
    loop_num = -1 //looping
  } else { //phone not moving
    loop_num = 0 //play once
  }
  rotate_method(note, sharp, stream, loop_num)
}

public void rotate_method(note, sharp, stream, loop_num){
  if(rotate){ //if sharp
    stream = soundpool.play (sharp, loop_num) //can be looping or not, depends on the loop_num
  } else { //not sharp
    stream = soundpool.play (note, loop_num)
  }
}

public void stop_audio_method(stream){
  stream = soundpool.stop() //stops that specific stream
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM