68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System;
|
|
|
|
namespace Lesson_One
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
// ========== 变量 ==========
|
|
int age = 25;
|
|
double height = 175.5;
|
|
string name = "无言势字幕";
|
|
char grade = 'A';
|
|
bool isActive = true;
|
|
|
|
Console.WriteLine("=== 变量 ===");
|
|
Console.WriteLine($"姓名: {name}");
|
|
Console.WriteLine($"年龄: {age}");
|
|
Console.WriteLine($"身高: {height}cm");
|
|
Console.WriteLine($"等级: {grade}");
|
|
Console.WriteLine($"状态: {isActive}");
|
|
|
|
// ========== var 自动推断 ==========
|
|
Console.WriteLine("\n=== var 自动推断 ===");
|
|
var message = "这是 string 类型";
|
|
var number = 42; // 推断为 int
|
|
Console.WriteLine($"message 是: {message.GetType()}");
|
|
Console.WriteLine($"number 是: {number.GetType()}");
|
|
|
|
// ========== 常量 ==========
|
|
Console.WriteLine("\n=== 常量 ===");
|
|
const int MAX_RETRY = 3;
|
|
const string VERSION = "v1.0";
|
|
Console.WriteLine($"最大重试次数: {MAX_RETRY}");
|
|
Console.WriteLine($"版本: {VERSION}");
|
|
|
|
// ========== 类型转换 ==========
|
|
Console.WriteLine("\n=== 类型转换 ===");
|
|
int i = 10;
|
|
double d = i; // 隐式转换
|
|
Console.WriteLine($"int -> double: {d}");
|
|
|
|
double d2 = 3.14;
|
|
int i2 = (int)d2; // 显式转换
|
|
Console.WriteLine($"double -> int: {i2}");
|
|
|
|
string s = i.ToString();
|
|
Console.WriteLine($"int -> string: {s}");
|
|
|
|
int i3 = int.Parse("123");
|
|
Console.WriteLine($"string \"123\" -> int: {i3}");
|
|
|
|
// ========== 输入练习 ==========
|
|
Console.WriteLine("\n=== 输入练习 ===");
|
|
Console.Write("请输入你的名字: ");
|
|
string inputName = Console.ReadLine();
|
|
Console.Write($"你好, {inputName}! ");
|
|
|
|
Console.Write("请输入年龄: ");
|
|
string ageInput = Console.ReadLine();
|
|
int userAge = int.Parse(ageInput);
|
|
Console.WriteLine($"你 {userAge} 岁了");
|
|
|
|
Console.ReadLine();
|
|
}
|
|
}
|
|
}
|