当前位置: 首页>>代码示例>>C#>>正文


C# Console.Beep方法代码示例

本文整理汇总了C#中System.Console.Beep方法的典型用法代码示例。如果您正苦于以下问题:C# Console.Beep方法的具体用法?C# Console.Beep怎么用?C# Console.Beep使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Console的用法示例。


在下文中一共展示了Console.Beep方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

// This example demonstrates the Console.Beep() method.
using System;

class Sample 
{
    public static void Main(String[] args) 
    {
    int x = 0;
//
    if ((args.Length == 1) &&
        (Int32.TryParse(args[0], out x) == true) &&
        ((x >= 1) && (x <= 9)))
        {
        for (int i = 1; i <= x; i++)
            {
            Console.WriteLine("Beep number {0}.", i);
            Console.Beep();
            }
        }
    else
        {
            Console.WriteLine("Usage: Enter the number of times (between 1 and 9) to beep.");
        }
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Console.Beep

输出:

>beep
Usage: Enter the number of times (between 1 and 9) to beep

>beep 9
Beep number 1.
Beep number 2.
Beep number 3.
Beep number 4.
Beep number 5.
Beep number 6.
Beep number 7.
Beep number 8.
Beep number 9.

示例2: Main

// This example demonstrates the Console.Beep(Int32, Int32) method
using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
// Declare the first few notes of the song, "Mary Had A Little Lamb".
    Note[] Mary = 
        {
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.GbelowC, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.HALF),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.HALF),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.D, Duration.QUARTER),
        new Note(Tone.D, Duration.HALF)
        };
// Play the song
    Play(Mary);
    }

// Play the notes in a song.
    protected static void Play(Note[] tune)
    {
    foreach (Note n in tune)
        {
        if (n.NoteTone == Tone.REST)
            Thread.Sleep((int)n.NoteDuration);
        else
            Console.Beep((int)n.NoteTone, (int)n.NoteDuration);
        }
    }

// Define the frequencies of notes in an octave, as well as 
// silence (rest).
    protected enum Tone
    {
    REST   = 0,
    GbelowC = 196,
    A      = 220,
    Asharp = 233,
    B      = 247,
    C      = 262,
    Csharp = 277,
    D      = 294,
    Dsharp = 311,
    E      = 330,
    F      = 349,
    Fsharp = 370,
    G      = 392,
    Gsharp = 415, 
    }

// Define the duration of a note in units of milliseconds.
    protected enum Duration
    {
    WHOLE     = 1600,
    HALF      = WHOLE/2,
    QUARTER   = HALF/2,
    EIGHTH    = QUARTER/2,
    SIXTEENTH = EIGHTH/2,
    }

// Define a note as a frequency (tone) and the amount of 
// time (duration) the note plays.
    protected struct Note
    {
    Tone     toneVal;
    Duration durVal;

// Define a constructor to create a specific note.
    public Note(Tone frequency, Duration time)
        {
        toneVal = frequency;
        durVal  = time;
        }

// Define properties to return the note's tone and duration.
    public Tone NoteTone { get{ return toneVal; } }
    public Duration NoteDuration { get{ return durVal; } }
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:90,代码来源:Console.Beep

输出:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.

示例3: Console.Beep()

//引入命名空间
using System;
using System.Windows.Forms;
using System.Media;

public class MainClass {
   public static void Main() {

       Console.Beep();

   }
}
开发者ID:C#程序员,项目名称:System,代码行数:12,代码来源:Console.Beep

示例4: Console.Beep(200, 300)

//引入命名空间
using System;
using System.Windows.Forms;
using System.Media;

public class MainClass {
   public static void Main() {
       Console.Beep(200, 300);
   }
}
开发者ID:C#程序员,项目名称:System,代码行数:10,代码来源:Console.Beep


注:本文中的System.Console.Beep方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。