【发布时间】:2016-12-08 17:40:27
【问题描述】:
我正在尝试制作一个程序,您可以在其中输入一个数字,然后它会生成一个数字轨迹。它将它分成数字并根据数字播放不同的哔声。但哔声之间存在延迟。这是项目的代码:
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
int[] numbersWithBeeps = new int [] { 262, 294, 330, 349, 392, 440, 494, 523, 600, 687, 878};
int input;
Console.WriteLine("Please enter the number you would like to play.");
input = Convert.ToInt32(Console.ReadLine());
input*=10;
List<int> actualNumbers = new List<int>();
do{
if(input%10 == 0)
{
actualNumbers.Add(0);
}
else
{
actualNumbers.Add(input%10);
}
input=input/10;
} while(input > 0);
for(int i = actualNumbers.Count - 1; i > 0; i--){
Console.Beep(numbersWithBeeps[actualNumbers[i]], 500);
}
}
}
}
【问题讨论】:
-
延迟多长时间(以毫秒为单位)?
-
您是否尝试过将第二个参数的哔哔声的持续时间更改为
Console.Beep?当我尝试它时,似乎每次哔哔声后都会有噪音,而不是真正的延迟,但在不同的硬件上运行时可能会有所不同。 -
请注意,根据this guy:“从单独的线程调用 Console.Beep() 会中断当前正在进行的任何操作。”,因此假设延迟只是
if上的字面延迟新的Beep调用中的语句迭代和延迟使其进入硬件,那么一个可能的答案是在一个线程中创建878哔声,然后在它仍在播放时创建687哔声在不同的线程中,它可以在 878->687 之间无延迟地播放它。 -
@DanWilson 好像是半秒左右 Juharr 是的,我试过了,但我认为不同的时间会有不同的延迟。我知道没有任何逻辑,但这是我现在可以向自己解释的唯一方法。 Quantic 我会试试这个并告诉你发生了什么