Csharp/C Sharp/Development Class/Console
Содержание
- 1 Beep
- 2 Beep(
- 3 Change console foreground color and background color
- 4 Clear console
- 5 CursorVisible ResetColor SetWindowSize BufferHeight BufferWidth CursorLeft CursorSize CursorTop
- 6 Displaying the sum of two numbers input from the keyboard.
- 7 Echo some stats
- 8 Printing multiple lines of text with string formatting.
- 9 Printing multiple lines with a single statement.
- 10 Printing one line of text with multiple statements.
- 11 Read a line of string and check its length
- 12 Set console: title, window size, buffer height and width, cursor position
- 13 Use Red and Green from ConsoleColor
- 14 WriteLine and Write
Beep
using System;
using System.Media;
class MainClass {
public static void Main() {
Console.Beep();
}
}
Beep(
using System;
using System.Media;
class MainClass {
public static void Main() {
// Play a beep with frequency as 200 and duration as 300
Console.Beep(200, 300);
}
}
Change console foreground color and background color
using System;
public class Test{
static void Main(string[] args){
Console.Title = "Standard Console";
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine("Press Enter to change the Console"s appearance.");
Console.ReadLine();
}
}
Clear console
using System;
public class Test{
static void Main(string[] args){
// Change the Console appearance and redisplay.
Console.Title = "Cleared / Colored Console";
Console.ForegroundColor = ConsoleColor.Blue;
Console.BackgroundColor = ConsoleColor.Yellow;
Console.Clear();
Console.WriteLine("Press Enter to change the Console"s appearance.");
Console.ReadLine();
}
}
CursorVisible ResetColor SetWindowSize BufferHeight BufferWidth CursorLeft CursorSize CursorTop
using System;
public class MainClass
{
static void Main(string[] args) {
// Change the console appearance and redisplay.
Console.Title = "Resized Console";
Console.ResetColor();
Console.Clear();
Console.SetWindowSize(100, 50);
Console.BufferHeight = 500;
Console.BufferWidth = 100;
Console.CursorLeft = 20;
Console.CursorSize = 50;
Console.CursorTop = 20;
Console.CursorVisible = false;
}
}
Displaying the sum of two numbers input from the keyboard.
using System;
public class Addition
{
public static void Main( string[] args )
{
int number1;
int number2;
int sum;
Console.Write( "Enter first integer: " );
number1 = Convert.ToInt32( Console.ReadLine() );
Console.Write( "Enter second integer: " );
number2 = Convert.ToInt32( Console.ReadLine() );
sum = number1 + number2;
Console.WriteLine( "Sum is {0}", sum );
}
}
Echo some stats
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
Console.Write("Enter your name: ");
string s;
s = Console.ReadLine();
Console.WriteLine("Hello, {0} ", s);
Console.Write("Enter your age: ");
s = Console.ReadLine();
Console.WriteLine("You are {0} years old", s);
}
}
Printing multiple lines of text with string formatting.
using System;
public class Welcome4
{
public static void Main( string[] args )
{
Console.WriteLine( "{0}\n{1}", "Welcome to", "C# Programming!" );
}
}
Printing multiple lines with a single statement.
using System;
public class Welcome3
{
public static void Main( string[] args )
{
Console.WriteLine( "Welcome\nto\nC#\nProgramming!" );
}
}
Printing one line of text with multiple statements.
using System;
public class Welcome2 {
public static void Main(string[] args) {
Console.Write("Welcome to ");
Console.WriteLine("C# Programming!");
}
}
Read a line of string and check its length
using System;
class MainEntryPoint {
static void Main(string[] args) {
Console.WriteLine("Type in a string");
string input;
input = Console.ReadLine();
if (input == "") {
Console.WriteLine("You typed in an empty string");
} else if (input.Length < 5) {
Console.WriteLine("The string had less than 5 characters");
} else if (input.Length < 10) {
Console.WriteLine("The string had at least 5 but less than 10 characters");
}
Console.WriteLine("The string was " + input);
}
}
Set console: title, window size, buffer height and width, cursor position
using System;
public class Test{
static void Main(string[] args){
// Change the Console appearance and redisplay.
Console.Title = "Resized Console";
Console.ResetColor();
Console.Clear();
Console.SetWindowSize(100, 50);
Console.BufferHeight = 500;
Console.BufferWidth = 100;
Console.CursorLeft = 20;
Console.CursorSize = 50;
Console.CursorTop = 20;
Console.CursorVisible = false;
Console.WriteLine("Main method complete. Press Enter.");
Console.ReadLine();
}
}
Use Red and Green from ConsoleColor
using System;
public class MainClass
{
static void Main(string[] args) {
// Change the console appearance and redisplay.
Console.Title = "Colored Text";
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
}
}
WriteLine and Write
using System;
class ConsoleAdder {
public static void Main() {
int a = 15;
int b = 744;
int c = a + b;
Console.Write("The sum of ");
Console.Write(a);
Console.Write(" and ");
Console.Write(b);
Console.Write(" equals ");
Console.WriteLine(c);
Console.WriteLine("The sum of " + a + " and " + b + " equals " + c);
Console.WriteLine("The sum of {0} and {1} equals {2}", a, b, c);
}
}