Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# visualc中的逻辑循环#_C#_Loops - Fatal编程技术网

C# visualc中的逻辑循环#

C# visualc中的逻辑循环#,c#,loops,C#,Loops,我正在编写一个小型音乐应用程序,并以这种方式每88个键播放一次: if (nutka == "A0" && msg.Velocity < 28) { PlayEngine.Instance.PlaySound("-12dB_01"); } else if (nutka == "A0" && msg.Velocity < 55 && msg.Velocity > 27) { PlayEngine.Instance.P

我正在编写一个小型音乐应用程序,并以这种方式每88个键播放一次:

if (nutka == "A0" && msg.Velocity < 28)
{
    PlayEngine.Instance.PlaySound("-12dB_01");
}
else if (nutka == "A0" && msg.Velocity < 55 && msg.Velocity > 27)
{
    PlayEngine.Instance.PlaySound("-9dB_01");
}
else if (nutka == "A0" && msg.Velocity < 82 && msg.Velocity > 54)
{
    PlayEngine.Instance.PlaySound("-6dB_01");
}
else if (nutka == "A0" && msg.Velocity < 106 && msg.Velocity > 81)
{
    PlayEngine.Instance.PlaySound("-3dB_01");
}
else if (nutka == "A0" && msg.Velocity < 128 && msg.Velocity > 105)
{
    PlayEngine.Instance.PlaySound("0dB_01");
}
if(nutka==“A0”&&msg.Velocity<28)
{
PlayEngine.Instance.PlaySound(“-12dB_01”);
}
否则如果(nutka==“A0”&&msg.Velocity<55&&msg.Velocity>27)
{
PlayEngine.Instance.PlaySound(“-9dB_01”);
}
否则如果(nutka==“A0”&&msg.Velocity<82&&msg.Velocity>54)
{
PlayEngine.Instance.PlaySound(“-6dB_01”);
}
否则如果(nutka==“A0”&&msg.Velocity<106&&msg.Velocity>81)
{
PlayEngine.Instance.PlaySound(“-3dB_01”);
}
否则如果(nutka==“A0”&&msg.Velocity<128&&msg.Velocity>105)
{
PlayEngine.Instance.PlaySound(“0dB_01”);
}
正如你们所看到的,我有5个速度范围,一个键的信号来自我的外部midi控制器。我有类似的88个if语句,唯一的变化是:nutka的名称和播放文件名称中的最后一个数字

(例如,在这里,我们可以使用5个文件播放一个音符“A0”,取决于速度:-12dB_01,-9dB_01,-6dB_01,-3dB_01和0dB_01,这在88个音符的代码中看起来非常糟糕


不知道如何制作较短的版本或可能是较短的循环…非常感谢您的帮助。

您通常会通过一个描述功能的项目列表来实现这一点

例如,给定一个简单的类

public class SoundInfo
{
    public string Nutka{get;set;}
    public int MinVelocity {get;set;}
    public int MaxVelocity {get;set;}
    public string SoundFile{get;set;}
}
将它们存储在
列表中


您通常会通过一个描述功能的项目列表来实现这一点

例如,给定一个简单的类

public class SoundInfo
{
    public string Nutka{get;set;}
    public int MinVelocity {get;set;}
    public int MaxVelocity {get;set;}
    public string SoundFile{get;set;}
}
将它们存储在
列表中


也许你可以把这根绳子连起来:

var keys = new Dictionary<string, string>();

// fill dictionary with relations: A0 -> 01
keys.Add("A0", "01");

var key  = keys[nutka];

int velocity;
if (msg.Velocity < 28)
    velocity = -12
else if (msg.Velocity < 55)
    velocity = -9
else if (msg.Velocity < 82)
    velocity = -6
else if (msg.Velocity < 106)
    velocity = -3
else
    velocity = 0;

string tune = String.Format("{0}dB_{1}", velocity, key);
PlayEngine.Instance.PlaySound(tune);
var keys=newdictionary();
//用关系填充字典:A0->01
添加(“A0”、“01”);
变量键=键[nutka];
内速度;
如果(消息速度<28)
速度=-12
否则,如果(消息速度<55)
速度=-9
否则,如果(消息速度<82)
速度=-6
否则,如果(消息速度<106)
速度=-3
其他的
速度=0;
stringtune=string.Format(“{0}dB{1}”,速度,键);
PlayEngine.Instance.PlaySound(tune);

字典的填充可以完成一次。

也许您可以将字符串压缩为:

var keys = new Dictionary<string, string>();

// fill dictionary with relations: A0 -> 01
keys.Add("A0", "01");

var key  = keys[nutka];

int velocity;
if (msg.Velocity < 28)
    velocity = -12
else if (msg.Velocity < 55)
    velocity = -9
else if (msg.Velocity < 82)
    velocity = -6
else if (msg.Velocity < 106)
    velocity = -3
else
    velocity = 0;

string tune = String.Format("{0}dB_{1}", velocity, key);
PlayEngine.Instance.PlaySound(tune);
var keys=newdictionary();
//用关系填充字典:A0->01
添加(“A0”、“01”);
变量键=键[nutka];
内速度;
如果(消息速度<28)
速度=-12
否则,如果(消息速度<55)
速度=-9
否则,如果(消息速度<82)
速度=-6
否则,如果(消息速度<106)
速度=-3
其他的
速度=0;
stringtune=string.Format(“{0}dB{1}”,速度,键);
PlayEngine.Instance.PlaySound(tune);

字典的填充可以完成一次。

一个列表和一些lamda将是你在这里的朋友!如果
nutka==“A1”
nutka==“B4”
?你能计算最后的字符串吗?一个列表和一些lamda将是你在这里的朋友!如果
nutka==“A1”
nutka==“B4”呢
?你能计算出最后一个字符串吗?谢谢你,杰米克。这看起来是让它工作并看起来“更短”的最好方法(Muhammad Zeeshan)-这样我需要为每个音符A0、A1、B0、B1再做87条语句……谢谢你,杰米克。这看起来是让它工作并看起来“更短”的最好方法:)穆罕默德·泽山——这样,我需要为每个音符A0、A1、B0、B1再做87次陈述。。。。。。。