我有一个伪代码,它可以播放不同的音频文件,我读到过一个嵌套的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个按钮,我需要这样做。我想使用哈希表,但创建列表,我会检查这样的条件,所以它会是相同的,不是吗?
我也找到了this,但是我这样分离条件不是和嵌套if一样吗?因为我有8个按钮,所以它也会很长很重复吗?
有没有办法缩短它,即使在所有的条件?
抱歉,我是新手。
2条答案
按热度按时间mepcadol1#
您可以将位掩码用于条件,并将其分组用于
switch
语句:alen0pnh2#
最后我把它们分成了不同的方法,如下所示: