https://www.youtube.com/watch?v=wxznTygnRfQ
学习网址
一、转义符的使用
1、\n换行 \t TAB \b回退
using System;
namespace MyFirstApplication{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\tHello, World!");
Console.Write("Hello\n, World!");
Console.WriteLine("Hello\b\b\b, World!");
//Console.Beep();
//Console.Beep();
Console.ReadKey();
}
}
}
二、转换格式
1、Convert.ToInt32();Convert.ToDouble()等
using System;
namespace MyFirstApplication{
class Program
{
static void Main(string[] args)
{
double a = 5.14;
int b = Convert.ToInt32(a);
Console.WriteLine("The value of b is: " + b);
Console.WriteLine("The value of a is: " + a);
Console.WriteLine("The type of a after conversion is: " + a.GetType());
Console.WriteLine("The type of b after conversion is: " + b.GetType());
Math.Round(a);
Random random = new Random();
}
}
}
三、math用法
1、Math.Round()四舍五入 ceiling向上取整,floor向下
using System;
namespace MyFirstApplication{
class Program
{
static void Main(string[] args)
{
double a = 3;
int b = Convert.ToInt32(a);
Console.WriteLine("The value of b is: " + b);
Console.WriteLine("The value of a is: " + a);
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The type of a after conversion is: " + a);
Console.WriteLine("The type of b after conversion is: " + b.GetType());
Console.WriteLine("The type of c after conversion is: " + c);
Console.WriteLine("The right answer is "+Math.Sqrt(Math.Pow(a,2)+ Math.Pow(c,2)));
Math.Round(a);
Random random = new Random();
String myPhoneNumber = "123-456-7890";
Console.WriteLine("My phone number is: " + myPhoneNumber);
myPhoneNumber = myPhoneNumber.Replace("-", "\\");
Console.WriteLine("Now my phone number is "+myPhoneNumber);
}
}
}
四、猜数游戏
using System;
namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
bool tryAgain = true;
int numberToGuess = 0;
int guess = 0;
int guesses = 0;
Random random = new Random();
while (tryAgain)
{
Console.WriteLine("Let's start playing a number guessing game!You'll try to guess a number between 1 and 100");
numberToGuess = random.Next(1, 101);
guess = Convert.ToInt32(Console.ReadLine());
guesses++;
while (guess != numberToGuess)
{
guesses++;
if (guess <= numberToGuess)
{
Console.WriteLine("your guess is too low!");
}
else if (guess >= numberToGuess)
{
Console.WriteLine("your guess is too high!");
}
guess = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("The number is "+ numberToGuess);
Console.WriteLine("You guessed the number in " + guesses + " tries!");
Console.WriteLine("Would you like to try again?[Y/N]");
String response = Console.ReadLine();
response = response.ToUpper();
if (response == "Y")
{
tryAgain = true;
guesses = 0; // Reset guesses for the next game
}
else if (response == "N")
{
tryAgain = false;
Console.WriteLine("Thanks for playing!");
}
else
{
Console.WriteLine("Invalid response. Exiting the game.");
tryAgain = false;
}
}
}
}
}
五、剪刀石头布
using System;
namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Let's play a game");
Random random = new Random();
bool tryAgain = true;
while (tryAgain)
{
int answer = random.Next(1, 4);
string answerToGuess = "";
String tryAgainInput = "";
switch (answer)
{
case 1:
answerToGuess = "ROCK";
break;
case 2:
answerToGuess = "PAPER";
break;
case 3:
answerToGuess = "SCISSORS";
break;
default:
break;
}
Console.WriteLine("Please enter your guess: ROCK, PAPER or SCISSORS.");
String input = Console.ReadLine();
input = input.Trim().ToUpper();
while ((input.Trim() == null) || (input != "ROCK" && input != "PAPER" && input != "SCISSORS"))
{
Console.WriteLine("Invalid input. Please enter ROCK, PAPER or SCISSORS.");
input = Console.ReadLine().Trim().ToUpper();
}
if ((answerToGuess == "PAPER" && input =="SCISSORS")|| (answerToGuess == "ROCK" && input == "PAPER")|| (answerToGuess == "SCISSORS" && input == "ROCK"))
{
Console.WriteLine("you win! you:" + input + " computer: " + answerToGuess);
}
else if(answerToGuess == input)
{
Console.WriteLine("It's a draw!"+ "you:" + input + " computer: " + answerToGuess);
}
else
{
Console.WriteLine("you lose! you:" + input + " computer: " + answerToGuess );
}
Console.WriteLine("would you like to try again? [Y/N]");
tryAgainInput = Console.ReadLine().Trim().ToUpper();
while (tryAgainInput != "Y" && tryAgainInput != "N")
{
Console.WriteLine("Invalid input. Please enter Y or N.");
tryAgainInput = Console.ReadLine().Trim().ToUpper();
}
tryAgain = tryAgainInput == "Y";
}
Console.WriteLine("Thank you for your playing!");
}
}
}
六、简易计算器
using System;
namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---------------------");
Console.WriteLine("Calculator");
Console.WriteLine("---------------------");
bool tryAgain = true;
do
{
Console.WriteLine("please enter num1");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("please enter num2");
double b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("please enter you option:\nnum1+num2: +\nnum1 - num2: -\nnum1 * num2: *\nnum1 / num2: /");
switch (Console.ReadLine())
{
case "+":
Console.WriteLine($"{a}+{b} answer is{a + b} ");
break;
case "-":
Console.WriteLine($"{a}-{b} answer is{a - b} ");
break;
case "*":
Console.WriteLine($"{a}*{b} answer is{a * b} ");
break;
case "/":
Console.WriteLine($"{a}/{b} answer is{a / b} ");
break;
default:
Console.WriteLine("invalid input !");
break;
}
Console.WriteLine("would you like to try again? [Y/N]");
tryAgain = Console.ReadLine().ToUpper() =="Y";
}while (tryAgain);
}
}
}
七、一维数组
tips:遍历数组foreach中有一个循环变量,一定要在循环中初始化,不能先在循环外初始化。
using System;
namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
String[] cars = new String[8];
cars[1] = "abc";
cars[2] = "def";
cars[5] = "qew";
for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}
foreach(String car in cars)
{
Console.WriteLine(car);
}
}
}
}
八、多参数处理&异常处理(不用try catch会直接终止进程)
1、SumUp(params double[] cars ){...}用于计算参数不确定的情况。
2、try{}catch{}catch{}finally{},无论如何finally中的语句一定会执行。
using System;
namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
Double[] doubles = new Double[10];
doubles[0] = 1.5;
Console.WriteLine($"double[0] = {doubles[0]}");
double result = SumUp(1,2,3.5,4.3,5.5,6.7,9.1);
Console.WriteLine(result);
try
{
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num1 / num2);
} catch (FormatException e)
{
Console.WriteLine("format exception!");
}
catch (DivideByZeroException e)
{ Console.WriteLine("divide by zero!"); }
finally
{
Console.WriteLine("thank you!");
}
}
static double SumUp(params double[] nums)
{
double total = 0;
foreach (double num in nums)
{
total = total + num;
}
return total;
}
}
}
九、左右对齐
1、Console.Write($"{cars[i, j],3}")右对齐,总共占三个空格的位置。
2、Console.Write($"{cars[i, j],-3}")左对齐,同上。
using System;
namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
String[,] cars = new String[8, 8];
for (int i = 0; i< cars.GetLength(0); i++){
for (int j = 0; j < cars.GetLength(1); j++)
{
cars[i,j] = Convert.ToString(i*j);
}
}
for (int i = 0;i< cars.GetLength(0); i++)
{
for(int j = 0;j< cars.GetLength(1); j++)
{
if (j == 0)
{
Console.Write($"{cars[i, j]}");
}
else
{
Console.Write($"{cars[i, j],3}");
}
}
Console.WriteLine();
}
}
}
}
0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21
0 4 8 12 16 20 24 28
0 5 10 15 20 25 30 35
0 6 12 18 24 30 36 42
0 7 14 21 28 35 42 49

526

被折叠的 条评论
为什么被折叠?



