Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters."); Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");
假设你的字符串中有你不想显示的前面或后面的空格。你想修剪字符串中的空格。 你可以直接使用 Trim 方法和相关的 TrimStart和 TrimEnd 方法来删除前导和尾部空格。
string songLyrics = "You say goodbye, and I say hello"; Console.WriteLine(songLyrics.Contains("goodbye")); Console.WriteLine(songLyrics.Contains("greetings"));
int a = 18; int b = 6; int c = a + b; int d = a - b; int e = a * b; int f = a / b; int g = a + b * c; int h = (a + b) - 6 * c + (12 * 4) / 3 + 12; Console.WriteLine($"{c}{d}{e}{f}{g}{h}");
你可以用余数运算符 % 找到余数。
1 2 3 4 5 6 7
int a = 7; int b = 4; int c = 3; int d = (a + b) / c; int e = (a + b) % c; Console.WriteLine($"quotient: {d}"); Console.WriteLine($"remainder: {e}");
由于整数在编码中的结构方式,其大小是有限制的。
1 2 3 4
int max = int.MaxValue; int min = int.MinValue; Console.WriteLine($"The range of integers is {min} to {max}"); // The range of integers is -2147483648 to 2147483647
双精度浮点型变量是数据的另一种表现形式。
1 2 3 4 5
double a = 19; double b = 23; double c = 8; double d = (a + b) / c; Console.WriteLine(d);
双精度浮点型变量的取值范围相当大,比整数大得多。
1 2 3 4
double max = double.MaxValue; double min = double.MinValue; Console.WriteLine($"The range of double is {min} to {max}"); // The range of double is -1.7976931348623157E+308 to 1.7976931348623157E+308
当然,双精度浮点型变量并不完美,它们也有四舍五入的误差。
1 2
double third = 1.0 / 3.0; Console.WriteLine(third);
decimal min = decimal.MinValue; decimal max = decimal.MaxValue; Console.WriteLine($"The range of the decimal type is {min} to {max}"); // The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335
数字的后缀 M 是表示常量应该使用 decimal 类型,数字的后缀 F 是表示常量应该使用 float 类型的方法。否则,编译器会默认假定为 Double 类型。
1 2 3 4 5 6 7 8 9 10 11
double a = 1.0; double b = 3.0; Console.WriteLine(a / b);
decimal c = 1.0M; decimal d = 3.0M; Console.WriteLine(c / d);
int a = 5; int b = 3; if (a + b > 10) Console.WriteLine("The answer is greater than 10"); else Console.WriteLine("The answer is not greater than 10");
如果你想在你的 if 语句中使用更复杂的代码,只要在你想做的事情周围加上大括号。
1 2 3 4 5 6 7 8 9 10 11
int c = 4; if ((a + b + c > 10) && (a == b)) { Console.WriteLine("The answer is greater than 10"); Console.WriteLine("And the first number is equal to the second"); } else { Console.WriteLine("The answer is not greater than 10"); Console.WriteLine("Or the first number is not equal to the second"); }
使用循环以重复执行语句。 while 语句会检查条件,并执行 while 后面的语句或语句块。 除非条件为 false,否则它会重复检查条件并执行这些语句。
1 2 3 4 5 6
int counter = 0; while (counter < 10) { Console.WriteLine($"Hello World! The counter is {counter}"); counter++; }
while 循环先测试条件,然后再执行 while 后面的代码。 do … while 循环先执行代码,然后再检查条件。
1 2 3 4 5 6
int counter = 0; do { Console.WriteLine($"Hello World! The counter is {counter}"); counter++; } while (counter < 10);
for 语句包含三个控制具体工作方式的部分。第一部分是 for 初始值设定项,中间部分是 for 条件,最后一部分是 for 迭代器。
1 2 3 4
for (int counter = 0; counter < 10; counter++) { Console.WriteLine($"Hello World! The counter is {counter}"); }
using System; using System.Collections.Generic; var names = new List<string> { "Sophia", "Ana", "Jayme", "Bill" }; string name = "Ana"; var index = names.IndexOf(name); Console.WriteLine($"Found {name} at {index}");
var account = new BankAccount("Kendra", 1000); Console.WriteLine($"Account{account.Number} was created for {account.Owner} with {account.Balance} dollars"); // Account was created for Kendra with 1000 dollars
var account = new BankAccount("Kendra", 1000); Console.WriteLine($"Account{account.Number} was created for {account.Owner} with {account.Balance} dollars"); // Account was created for Kendra with 1000 dollars
var account = new BankAccount("Kendra", 1000); Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance} dollars"); // Account 1234567890 was created for Kendra with 1000 dollars
var account = new BankAccount("Kendra", 1000); Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance} dollars"); // Account 1234567890 was created for Kendra with 0 dollars
// Functions publicvoidMakeDeposit(decimal amount, DateTime date, string note) { //(#2) if (amount <= 0) { thrownew ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive"); } //(#1) var deposit = new Transaction(amount, date, note); allTransactions.Add(deposit); }
publicvoidMakeWithdrawal(decimal amount, DateTime date, string note) { //(#3) if (amount <= 0) { thrownew ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive"); } if (Balance - amount < 0) { thrownew InvalidOperationException("Not sufficient funds for this withdrawal"); } var withdrawal = new Transaction(-amount, date, note); allTransactions.Add(withdrawal); } }
var account = new BankAccount("Kendra", 1000); Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance} dollars");
account.MakeWithdrawal(500, DateTime.Now, "Rent payment"); //Added test code Console.WriteLine(account.Balance); account.MakeDeposit(100, DateTime.Now, "Friend paid me back"); Console.WriteLine(account.Balance);
/* Account 1234567890 was created for Kendra with 1000 dollars 500 600 */