【6月日新计划01】C#入门介绍-簡單運算,邏輯判斷
C# 实际上是微软的旗舰语言。它是相当通用的——你可以用它来编写 各种程序,从传统的控制台和桌面应用,通过网站和服务到移动开发, 无论是商业还是娱乐。
C# 开发工具: Visual Studio。
1. Hello C#
using System;
namespace My_first_program
{
class Program
{
static void Main(string[] args)
{
// Output of text to the user
Console.WriteLine("I am starting to program in C#.");
// Waiting for Enter
Console.ReadLine();
}
}
}
特殊字符,需要转义
using System;
namespace My_first_program
{
class Program
{
static void Main(string[] args)
{
// Multiline output
Console.WriteLine("First line\r\nSecond line");
Console.ReadLine();
}
}
}
多行文本,在文本的左引号前加上(@)符号,注意(@)符号也关闭转义序列
2. Object
2.1 DateTime
显示当前日期和时间
static void Main(string[] args)
{
// Current date and time (using single statement)
DateTime now = DateTime.Now;
// Picking up individual components
int day = now.Day;
int month = now.Month;
int year = now.Year;
int hours = now.Hour;
int minutes = now.Minute;
int seconds = now.Second;
DateTime justDateWithoutTime = now.Date;
// Output Console.WriteLine("Day: " + day);
Console.WriteLine("Month: " + month);
Console.WriteLine("Year: " + year);
Console.WriteLine("Hours: " + hours);
Console.WriteLine("Minutes: " + minutes);
Console.WriteLine("Seconds: " + seconds);
Console.WriteLine("Our output: " + year + ", " + month + "/" + day + " " + hours + " hours " + minutes + " minutes");
// Waiting for Enter
Console.ReadLine(); }
仅显示日期
static void Main(string[] args)
{
// Variable of DateTime type, at first empty
DateTime today;
// Storing of today's date (without time component)
today = DateTime.Today;
Console.WriteLine("Today is " + today);
DateTime tomorrow = today.AddDays(1);
Console.WriteLine("Tomorrow is " + tomorrow);
// Waiting for Enter
Console.ReadLine(); }
2.2 Random
static void Main(string[] args)
{
// Creating random number generator object
Random randomNumbers = new Random();
// Repeatedly throwing a die
int number1 = randomNumbers.Next(1, 6 + 1);
int number2 = randomNumbers.Next(1, 6 + 1);
int number3 = randomNumbers.Next(1, 6 + 1);
// Output
Console.WriteLine("Thrown numbers: " + number1 + ", " + number2 + ", " + number3);
// Waiting for Enter
Console.ReadLine(); }
2.3 Console
static void Main(string[] args)
{
// Hinting user what we want from her
Console.Write("Enter a sentence (and press Enter):");
// Reading line of text
string input = Console.ReadLine();
// Repeating to the output
Console.WriteLine("You have entered: " + input);
// Waiting for Enter
Console.ReadLine();
}
2.4 Basic Operation
数字运算
static void Main(string[] args)
{
// Inputs
Console.Write("Enter first number: ");
string input1 = Console.ReadLine();
double number1 = Convert.ToDouble(input1);
Console.Write("Enter second number: ");
string input2 = Console.ReadLine();
double number2 = Convert.ToDouble(input2);
// Calculations
double sum = number1 + number2;
double difference = number1 - number2;
double product = number1 * number2;
double quotient = number1 / number2;
// Output
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Difference is " + difference);
Console.WriteLine("Product is " + product);
Console.WriteLine("Quotient is " + quotient);
// Waiting for Enter
Console.ReadLine();
}
日期运算
static void Main(string[] args)
{
// Input
Console.Write("Enter your date of birth: ");
string input = Console.ReadLine();
DateTime dateOfBirth = Convert.ToDateTime(input);
// Today
DateTime today = DateTime.Today;
// Date difference
TimeSpan difference = today - dateOfBirth;
int numberOfDays = difference.Days;
// Output
Console.WriteLine();
Console.WriteLine("Today is: " + today.ToShortDateString Console.WriteLine("The world likes you for this number of days: "
// Waiting for Enter Console.ReadLine(); }
时区和UTC
static void Main(string[] args)
{
// Current time serves as input
DateTime now = DateTime.Now;
DateTime utcNow = DateTime.UtcNow;
DateTimeOffset completeInstant = DateTimeOffset.Now;
DateTimeOffset utcCompleteInstant = DateTimeOffset.UtcNow;
// Outputs
Console.WriteLine("Now: " + now);
Console.WriteLine("UTC now: " + utcNow);
Console.WriteLine("Now (including time zone): " + completeInstant
Console.WriteLine("Time zone (offset against UTC): " + utcCompleteInstant
// Waiting for Enter
Console.ReadLine(); }
3. Conditions
3.1 if
if...else....
if (user1ok || user2ok) {
Console.WriteLine("Thanks for your books!");
} else {
Console.WriteLine("Could not log you in.");
}
3.2 logic
&& ||
if (user1ok || user2ok) {
Console.WriteLine("Thanks for your books!");
} else {
Console.WriteLine("Could not log you in.");
}
4. Loop
static void Main(string[] args)
{
// Output
for (int count = 0; count < 10; count++) {
Console.WriteLine("I will start learning tomorrow."
}
// Waiting for Enter
Console.ReadLine();
}
4.1 反复投骰子
static void Main(string[] args)
{
// Random number generator
Random randomNumbers = new Random();
// Output,next方法1是下限,6是上限
for (int count = 0; count < 20; count++) {
int thrown = randomNumbers.Next(1, 6 + 1);
Console.Write(thrown.ToString() + " ");
}
// Waiting for Enter
Console.ReadLine();
}
5. 打印對象屬性
public void PropertyList(this object obj)
{
var props = obj.GetType().GetProperties();
var sb = new StringBuilder();
foreach (var p in props)
{
sb.AppendLine(p.Name + ": " + p.GetValue(obj, null));
}
System.Diagnostics.Trace.WriteLine(sb.ToString());
}