C# 入门学习教程(二)

文章目录

  • 一、操作符详解
    • 1、操作符概览
    • 2、操作符的本质
    • 3、操作符的优先级
    • 4、同级操作符的运算顺序
    • 5、 各类操作符的示例
  • 二、表达式,语句详解
    • 1. 表达式的定义
    • 2. 各类表达式概览
    • 3. 语句的定义
    • 4. 语句详解

一、操作符详解

C# 中的操作符是用于执行程序代码运算的符号,它们可以对一个或多个操作数进行操作并返回结果。

1、操作符概览

类别运算符
基本x.y f(x) a[x] x++ x-- new typeof default checked unchecked delegate sizeof ->
一元+ - ! ~ ++x --x (T)x await &x *x
乘法* / %
加减+ -
移位>> <<
关系和类型检测< > <= >= is as
相等== !=
逻辑“与”&
逻辑 XOR^
逻辑 OR|
条件 AND&&
条件 OR||
null 合并??
条件?:
lambda 表达式= *= /= %= += -= <<= >>= &= ^= |= =>
  • 操作符(Operator )也译为“运算符”
  • 操作符是用来操作数据的,被操作符操作的数据称为操作数( Operand )

2、操作符的本质

  • 操作符的本质是函数(即算法)的“简记法”

    • 假如没有发明“+”、只有Add函数,算式3+4+5将可以写成Add(Add(3,4),5)

    • 假如没有发明“x”、只有Mul函数,那么算式3+4x5将只能写成Add(3,Mul(4,5)),注意优先级

  • 操作符不能脱离与它关联的数据类型

    • 可以说操作符就是与固定数据类型相关联的一套基本算法的简记法
    namespace CreateOperator
    {class Program{static void Main(string[] args){int x = 5;int y = 4;int z = x / y;Console.WriteLine(z);double x1 = 5;double y1 = 4;double z1 = x1 / y1;Console.WriteLine(z1);}}
    }
    • 示例:为自定义数据类型创建操作符
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;namespace CreateOperator
    {class Program{static void Main(string[] args){//List<Person> nation = Person.GetMarry(persion1,persion2);//foreach( var p in nation){//    Console.WriteLine(p.Name);//}List<Person> nation = persion1 + persion2;foreach( var p in nation){Console.WriteLine(p.Name);}}}class Person{public string Name;public static List<Person> operator +(Person p1, Person p2){List<Person> people = new List<Person> { };people.Add(p1);people.Add(p2);for (int i = 0; i < 11; i++){Person child = new Person();child.Name = p1.Name + '&' + p2.Name;people.Add(child);}return people;}}
    }

3、操作符的优先级

  • 操作符的优先级
    • 可以使用圆括号提高被括起来表达式的优先级
    • 圆括号可以嵌套
    • 不像数学里有方括号和花括号,在C#语言里“0”与"0”有专门的用途

4、同级操作符的运算顺序

  • 除了带有赋值功能的操作符,同优先级操作符都是由左向右进行运算
    • 有赋值功能的操作符的运算顺序是由右向左
    • 与数学运算不同,计算机语言的同优先级运算没有“结合率”
      • 3+4+5只能理解为Add(Add(3,4),5)不能理解为Add(3,Add(4,5))

5、 各类操作符的示例

. 操作符

namespace OperatorExample
{class Program{static void Main(string[] args){//. 操作符Form a = new Form();a.Text = "my form";a.ShowDialog();}}
}

F(x) 函数调用操作符

namespace OperatorExample
{class Program{static void Main(string[] args){//F(x) 函数调用操作符calure cc = new calure();int dd = cc.Add(1, 2);Console.WriteLine(dd);//委托Action ee = new Action(cc.print);ee();}}class calure{public int Add(int a, int b){return a + b;}public void print(){Console.WriteLine("Hello");}}
}

a[x]

namespace OperatorExample
{class Program{static void Main(string[] args){int[] intarray = new int[10];console.writeline(intarray[1]);int[] intarray2 = new int[5]{11,22,33,44,55};console.writeline(intarray2[3]);dictionary<string,student> stdob = new dictionary<string,student>();for (int i = 0; i < 101; i++){student stu = new student();stu.name = "学生" + i.tostring();stu.score = 100 + i;stdob.add(stu.name,stu);}console.writeline(stdob["学生12"].name +"; "+ stdob["学生11"].score);}}class student{public string Name;public int Score;}}

x++ x–

namespace OperatorExample
{class Program{static void Main(string[] args){int x = 100;x++;Console.WriteLine(x);x--;Console.WriteLine(x);int y = x++;Console.WriteLine(x);Console.WriteLine(y);}}
}

typeof

namespace OperatorExample
{class Program{static void Main(string[] args){Type x = typeof(int);Console.WriteLine(x.Name);Console.WriteLine(x.FullName);Console.WriteLine(x.Namespace);int a = x.GetMethods().Length;Console.WriteLine(a);foreach (var item in x.GetMethods()){Console.WriteLine(item.Name);}}}
}

default

namespace OperatorExample
{class Program{static void Main(string[] args){int a = default(int);Console.WriteLine(a);Form a = default(Form);Console.WriteLine(a == null);leved ll = default(leved);Console.WriteLine(ll);leved22 dd = default(leved22);Console.WriteLine(dd);}enum leved{mid,low,High}enum leved22{mid = 1,low = 2,High = 3,def = 0}}
}

new

namespace OperatorExample
{class Program{static void Main(string[] args){Form myForm = new Form();myForm.Text = "hello";myForm.ShowDialog();Form myFormOne = new Form() { Text = "my text" };myFormOne.ShowDialog();// var 自动推断类型var myTx = new { text = "1234", age = 55 };Console.WriteLine(myTx.text);Console.WriteLine(myTx.age);Console.WriteLine(myTx.GetType().Name);}}
}

checked uncheckded

namespace OperatorExample
{class Program{static void Main(string[] args){uint x = uint.MaxValue;Console.WriteLine(x);string aa = Convert.ToString(x, 2);Console.WriteLine(aa);//检测checked{try{uint y = x + 1;Console.WriteLine(y);}catch (OverflowException ex){//捕获异常Console.WriteLine("异常");}}}}
}

**delegate **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace ddExample
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.myClick.Click += myClick_Click;//delegatethis.myClick.Click += delegate (object sender, RoutedEventArgs e){this.myTextBox.Text = "1111111111";};this.myClick.Click += (object sender, RoutedEventArgs e)=>{this.myTextBox.Text = "1111111111";};}void myClick_Click(object sender, RoutedEventArgs e){this.myTextBox.Text = "1111111111";}}
}

sizeof , ->

namespace OperatorExample
{class Program{static void Main(string[] args){int x = sizeof(int);Console.WriteLine(x);int x1 = sizeof(decimal);Console.WriteLine(x1);unsafe{//打印自定义数据结构的长度int y = sizeof(StudentStr);Console.WriteLine(y);//箭头操作符,操作内存StudentStr stu;stu.age = 16;stu.score = 100;StudentStr* pStu = &stu;pStu->score = 99;Console.WriteLine(stu.score);}}}struct StudentStr{public int age;public long score;}
}

+ - 正负

namespace OperatorExample
{class Program{static void Main(string[] args){int x = 100;Console.WriteLine(x);int y = -100;Console.WriteLine(y);int z = -(-x);Console.WriteLine(z);int x1 = int.MinValue;int y1 = -x1;Console.WriteLine(x1);Console.WriteLine(y1);//溢出int y2 = checked(-x1);}}
}

~ 求反

namespace OperatorExample
{class Program{static void Main(string[] args){int x = int.MinValue;int y = ~x;Console.WriteLine(x);Console.WriteLine(y);string xstr = Convert.ToString(x, 2).PadLeft(3, '0');string ystr = Convert.ToString(y, 2).PadLeft(3, '0');Console.WriteLine(xstr);Console.WriteLine(ystr);}}
}

bool

namespace OperatorExample
{class Program{static void Main(string[] args){bool x = false;bool y = x;Console.WriteLine(x);Console.WriteLine(y);StudentTwo tt = new StudentTwo(null);Console.WriteLine(tt.Name);}}class StudentTwo{public string Name;public StudentTwo(string Name){if (!string.IsNullOrEmpty(Name)){this.Name = Name;}else{throw new ArgumentException("name is empty");}}}
}

++x --x

namespace OperatorExample
{class Program{static void Main(string[] args){int x = 100;int y = ++x;Console.WriteLine(x);Console.WriteLine(y);int x1 = 100;int z = x1++;Console.WriteLine(z);}}
}

(T)x 类型转换

  • 隐式(implicit)类型转换

    • 不丢失精度的转换
    • 子类向父类的转换
    • 装箱显式(explicit)类型转换
  • 有可能丢失精度(甚至发生错误)的转换,即cast拆箱使用Convert

    • ToString方法与各数据类型的Parse/TryParse方法自定义类型转换操作符示例隐式(implicit)类型转换
    • 不丢失精度的转换
    • 子类向父类的转换
    • 装箱显式(explicit)类型转换
  • 有可能丢失精度(甚至发生错误)的转换,即cast拆箱使用Convert类

    • ToString方法与各数据类型的Parse/TryParse方法自定义类型转换操作符示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){//隐式类型转换//不丢失精度的转换int x = int.MaxValue;long y = x;Console.WriteLine(y);// 子类向父类进行的隐式转换Teacher t = new Teacher();Human h = t;h.Eat();h.Think();Animal a = h;a.Eat();//显式类型转Console.WriteLine(ushort.MaxValue);uint x = 65536;ushort y = (ushort)x;Console.WriteLine(y);}class Animal{public void Eat(){Console.WriteLine("Eating....");}}class Human : Animal{public void Think(){Console.WriteLine("Think ...");}}class Teacher : Human{public void Teach(){Console.WriteLine("Teacher");}}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace ConertExample
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void button_1_Click(object sender, RoutedEventArgs e){double x = System.Convert.ToDouble(TextBox_1.Text);double y = System.Convert.ToDouble(TextBox_2.Text);double z = x + y;//TextBox_3.Text = System.Convert.ToString(z);TextBox_3.Text = z.ToString();double x1 = double.Parse(TextBox_1.Text);double y1 = double.Parse(TextBox_2.Text);double z1 = x1 + y1;//TextBox_3.Text = System.Convert.ToString(z);TextBox_3.Text = z1.ToString();}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace C0onvertExplameT
{class Program{static void Main(string[] args){stone ss = new stone();ss.Age = 10;//explicitMonkey mm = (Monkey)ss;//implicitMonkey mm = ss;Console.WriteLine(mm.Age);}}class stone{public int Age;//显式类型转换操作符 explicit public static explicit operator Monkey(stone s){Monkey aa = new Monkey();aa.Age = s.Age * 100;return aa;}//隐式类型转换  implicit//public static implicit operator Monkey(stone s)//{//    Monkey aa = new Monkey();//    aa.Age = s.Age * 200;//    return aa;//}}class Monkey{public int Age;}
}

is as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){// is运算符Teacher t = new Teacher();var res = t is Teacher;Console.WriteLine(res);Console.WriteLine(res.GetType().FullName);var res1 = t is Human;Console.WriteLine(res1);var res2 = t is object;Console.WriteLine(res2);// as 运算符object t = new Teacher();Teacher tt = t as Teacher;if (tt != null){tt.Teach();}}class Animal{public void Eat(){Console.WriteLine("Eating....");}}class Human : Animal{public void Think(){Console.WriteLine("Think ...");}}class Teacher : Human{public void Teach(){Console.WriteLine("Teacher");}}}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 5;int y = 6;console.writeline(x * y);double a = 5.0;double b = 4.0;var z = a * b;console.writeline(z);console.writeline(z.gettype().name);int a = 5;double b = a;console.writeline(b.gettype().name);int x1 = 5;double y1 = 2.5;console.writeline(x1 * y1);var z = x1 * y1;console.writeline(z.gettype().name);}}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 5;int y = 4;Console.WriteLine(x / y);double x1 = 5.0;double y1 = 4.0;Console.WriteLine(x1 / y1);double a = double.PositiveInfinity;double b = double.NegativeInfinity;Console.WriteLine(a / b);double c = (double)5 / 4;Console.WriteLine(c);double d = (double)(5 / 4);Console.WriteLine(d);}}
}

取余 %

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){for (int i = 0; i < 100; i++){Console.WriteLine(i % 10);}double x = 3.5;double y = 3;Console.WriteLine(x % y);   }}
}

加法 + ,减法 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){//加法 +int x = 5;double y = 1.5;console.writeline(x + y);console.writeline((x + y).gettype().name);string a = "123";string b = "abc";console.writeline(a + b);//减法 -int x = 10;double y = 5.0;var z = x - y;Console.WriteLine(z);Console.WriteLine(z.GetType().Name);   }}
}

位移 >> <<

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){//位移,左移一位即原值*2int x = 7;int y = x << 2;Console.WriteLine(y);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');Console.WriteLine(xStr);string yStr = Convert.ToString(y, 2).PadLeft(32, '0');Console.WriteLine(yStr);//右移一位即原值/2int x = 100;int y = x >> 2;Console.WriteLine(y);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');Console.WriteLine(xStr);string yStr = Convert.ToString(y, 2).PadLeft(32, '0');Console.WriteLine(yStr);//一直位移的情况下,会出现类型溢出的情况,需要checked}}
}

**比较运算符 <> == != **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int a = 10;int b = 5;var c = a < b;Console.WriteLine(c);Console.WriteLine(c.GetType().FullName);char 类型比较大小,转换成ushort 比较大小char a = 'a';char b = 'A';var res = a > b;Console.WriteLine(res);ushort au = (ushort)a;ushort bu = (ushort)b;Console.WriteLine(au);Console.WriteLine(bu);//对齐后,挨个比较unicodestring str1 = "abc";string str2 = "Abc";Console.WriteLine(str1.ToLower() == str2.ToLower());}}
}

**逻辑“与”& ; 逻辑XOR ^ ; 逻辑 OR | **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 7;int y = 10;// 按位求与,二进制1为真,0为假,真假为假,真真为真,假假为假int z = x & y;Console.WriteLine(z);string xStr = Convert.ToString(x,2).PadLeft(32,'0');string yStr = Convert.ToString(y,2).PadLeft(32,'0');string zStr = Convert.ToString(z,2).PadLeft(32,'0');Console.WriteLine(xStr);Console.WriteLine(yStr);Console.WriteLine(zStr);// 按位求或,二进制1为真,0为假,真假为真,真真为真,假假为假int z = x | y;Console.WriteLine(z);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');string yStr = Convert.ToString(y, 2).PadLeft(32, '0');string zStr = Convert.ToString(z, 2).PadLeft(32, '0');Console.WriteLine(xStr);Console.WriteLine(yStr);Console.WriteLine(zStr);// 按位求异或,二进制1为真,0为假,不一样为真,一样为假int z = x ^ y;Console.WriteLine(z);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');string yStr = Convert.ToString(y, 2).PadLeft(32, '0');string zStr = Convert.ToString(z, 2).PadLeft(32, '0');Console.WriteLine(xStr);Console.WriteLine(yStr);Console.WriteLine(zStr);}}
}

条件与 && 条件或 ||

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 10;int y = 5;double a = 10.5;double b = 5;if(x>y && a>b){Console.WriteLine("真");}else{Console.WriteLine("假");}if (x < y || a > b){Console.WriteLine("真");}else{Console.WriteLine("假");}}}
}

null合并 ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int? x = null;//x = 100;Console.WriteLine(x);Console.WriteLine(x.Value);int y = x ?? 1;Console.WriteLine(y);}}
}

条件 ?:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 60;string str = x >= 60 ? "及格" : "不及格 ";Console.WriteLine(str);}}
}

**赋值 *= += -= /= %= <<= >>= &= ^= |= **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int a = 100;int b = a += 10;Console.WriteLine(b);int c = a>>=1;Console.WriteLine(c);}}
}

二、表达式,语句详解

1. 表达式的定义

  • 什么是表达式
    在 C# 里,表达式是由操作数和运算符组合而成的代码片段,它能计算出一个值。操作数可以是变量、常量、字面量、方法调用等;运算符则用于指明对操作数进行何种运算。

  • C#语言对表达式的定义

    • 算法逻辑的最基本(最小)单元,表达一定的算法意图
    • 因为操作符有优先级,所以表达式也就有了优先级

2. 各类表达式概览

  • C#语言中表达式的分类

    • A value. Every value has an associated type.任何能得到值的运算(回顾操作符和结果类型)
    • A variable. Every variable has an associated type.
    int x = 10;
    int y;
    y = x;
    
    • A namespace.
    //A namespace
    System.Windows.Forms.Form myForm;
    
    • A type.
    // A typevar t = typeof(Int32);
    
    • A method group.例如:Console.WriteLine,这是一组方法,重载决策决定具体调用哪一个
     //A method groupConsole.WriteLine("Hello");
    

    Console.WriteLine 有19个方法供使用

    在这里插入图片描述

    • A null literal.
         //A null literalForm MyForm = null;
    
    • An anonymous function.
    Action a = delegate () { Console.WriteLine("hello"); };
    a();
    
    • A property access.
       // a propety accessForm my = new Form();my.Text = "my Text";my.ShowDialog();
    
    • An event access.
    static void Main(string[] args)
    {Form myFo = new Form();myFo.Load += myFo_Load;myFo.ShowDialog();
    }static void myFo_Load(object sender, EventArgs e)
    {Form form = sender as Form;if (form == null){return;}form.Text = "new Text";
    }
    
    • An indexer access.
     List<int> listone = new List<int> { 1, 22, 333 };int xOne = listone[2];
    
    • Nothing.对返回值为void的方法的调用
     //NothingConsole.WriteLine("Hello");
    

在这里插入图片描述

  • 复合表达式的求值
    • 注意操作符的优先级和同优先级操作符的运算方向

3. 语句的定义

  • Wikipedia对语句的定义

    语句是高级语言的语法

    编译语言和机器语言只有指令(高级语言中的表达式对应低级语言中的指令),语句等价于一个或一组有明显逻辑关联的指令

  • C#语言对语句的定义

    • C#语言的语句除了能够让程序员“顺序地”(sequentially)表达算法思想,还能通过条件判断、跳转和循环等方法控制程序逻辑的走向
    • 简言之就是:陈述算法思想,控制逻辑走向,完成有意义的动作(action)
    • C#语言的语句由分号(;)结尾,但由分号结尾的不一定都是语句
    • 语句一定是出现在方法体

4. 语句详解

  • 声明语句
int xSF = 100;
var x1 = 200;
const int xC = 100;
  • 表达式语句
Console.WriteLine("Hello World");new Form();int xT = 100;
xT = 200;xT++;
xT--;
++xT;
--xT;
  • 块语句
{int XTo = 100;if (XTo < 100)Console.WriteLine("不及格");
}
  • 标签语句
 {hello: Console.WriteLine("Hello World");goto hello;}
  • 块语句的作用域
 int ykuai = 100;{int xkuai = 200;Console.WriteLine(ykuai);}// 无法在块语句外打印xkuaiConsole.WriteLine(xkuai);
  • if 语句
 int score = 59;if (score >= 80){Console.WriteLine("A");}else if (score >= 60){Console.WriteLine("B");}else if (score >= 40){Console.WriteLine("C");}else{Console.WriteLine("D");}
  • switch 语句
static void Main(string[] args)
{Level myLevel = new Level();switch (myLevel){case Level.High:Console.WriteLine(Level.High);break;case Level.Mid:Console.WriteLine(Level.Mid);break;case Level.Low:Console.WriteLine(Level.Low);break;default:Console.WriteLine("凉凉");break;}
}enum Level
{High,Mid,Low
}
  • try 语句,尽可能多的去处理异常
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace ExpressionExample
{class Program{static void Main(string[] args){Count cc = new Count();int res = cc.Add("122222222", "122222222");Console.WriteLine(res);}}class Count{public int Add(string a, string b){int x = 0;int y = 0;bool error = false;try{x = int.Parse(a);y = int.Parse(b);}catch (ArgumentNullException ex){Console.WriteLine(ex.Message);error = true;}catch (FormatException ex){Console.WriteLine(ex.Message);error = true;}catch (OverflowException ex){Console.WriteLine(ex.Message);error = true;}finally{if (error){Console.WriteLine("有错误不能计算");}else{Console.WriteLine("一切正常");}}int z = x + y;return z;}}
}
  • while 循环语句
namespace ExpressionExample
{class Program{static void Main(string[] args){bool next = true;while (next){Console.WriteLine("请输入数字1:");string str1 = Console.ReadLine();int a1 = int.Parse(str1);Console.WriteLine("请输入数字2:");string str2 = Console.ReadLine();int a2 = int.Parse(str2);if (a1 + a2 == 100){next = false;Console.WriteLine("已经 100");}else{Console.WriteLine("请继续");}}}}
}
  • do while 循环语句
namespace ExpressionExample
{class Program{static void Main(string[] args){bool nextDo = false;do{Console.WriteLine("请输入数字1:");string str1 = Console.ReadLine();int a1 = 0;try{a1 = int.Parse(str1);}catch (Exception ex){Console.WriteLine(ex.Message);continue;}Console.WriteLine("请输入数字2:");string str2 = Console.ReadLine();int a2 = 0;try{a2 = int.Parse(str2);}catch (Exception ex){Console.WriteLine(ex.Message);continue;}if (a1 == 100 || a2 == 100){Console.WriteLine("100 直接退出");break;}if (a1 + a2 == 100){nextDo = false;Console.WriteLine("已经 100");}else{nextDo = true;Console.WriteLine("请继续");}} while (nextDo);}}
}
  • for 循环语句,99乘法表
namespace ExpressionExample
{class Program{static void Main(string[] args){for (int i = 1; i < 10; i++){for (int j = 1; j < 10; j++){if (i >= j){int z = i * j;Console.Write("{0} * {1} = {2} ", i, j, z);}else{Console.WriteLine("");break;}}}}}
}
  • 迭代器 与 foreach
namespace ExpressionExample
{class Program{static void Main(string[] args){int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };IEnumerator values = ints.GetEnumerator(); //指月while (values.MoveNext()){Console.WriteLine(values.Current);}//重置values.Reset();while (values.MoveNext()){Console.WriteLine(values.Current);}// foreach 语句foreach (int i in ints){Console.WriteLine(i);}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/88223.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/88223.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux内核深度解析:IPv4策略路由的核心实现与fib_rules.c源码剖析

深入探索Linux网络栈的规则引擎,揭秘策略路由如何通过多级路由表实现复杂流量控制 在Linux网络栈中,路由决策远不止简单的目的地址匹配。策略路由(Policy Routing)允许根据源地址、TOS值、端口等复杂条件选择不同的路由路径。本文将深入剖析实现这一功能的核心源码——net/…

【UE5】虚幻引擎的运行逻辑

UE5的运行逻辑可以分为引擎启动流程和游戏运行流程两个部分。引擎启动流程一、平台入口&引擎主流程初始化1、系统入口不同的平台会有不同的入口。在Windows平台&#xff0c;入口是Launch模块下的\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp文件中的W…

大数据学习1:Hadoop单机版环境搭建

1.基础知识介绍 Flume采集日志。Sqoop采集结构化数据&#xff0c;比如采集数据库。 存储到HDFS上。 YARN资源调度&#xff0c;每台服务器上分配多少资源。 Hive是基于Hadoop的一个数据仓库工具&#xff0c;提供SQL查询功能&#xff0c;能将SQL语句转变成MapReduce任务来执行…

深入理解PHP中的命名空间和自动加载机制

首先&#xff0c;让我们来讨论命名空间。PHP的命名空间是一种对代码进行逻辑分组的机制&#xff0c;它允许开发者将函数、类和常量封装在不同的命名空间中。这样做的好处在于可以避免全局范围内的名称冲突。例如&#xff0c;你可能在你的项目中使用了一个名为"Database&qu…

学习:JS[3]数组的增删改查+函数+作用域

一.操作数组1.改2.增arr.push(新增的内容):将一个或多个元素添加到数组的结尾arr.unshift(新增的内容):方法将一个或多个元素添加到数组的开头,并返回该数组的长度3.删除arr.pop():方法从数组中删除最后一个元素,不带参数,并返回元素的值arr.shift():方法从数组中删除第一个元素…

从0到1搭建ELK日志收集平台

ELK是什么 ELK 是指 Elasticsearch、Logstash 和 Kibana 这三种工具的组合&#xff0c;通常用于日志分析、数据搜索和可视化。它们分别承担不同的功能&#xff0c;形成了强大的数据处理和分析平台&#xff1a; Elasticsearch&#xff1a;一个分布式搜索引擎&#xff0c;擅长实时…

Qt:图片切割

void MainWindow::on_action_slice_triggered() {QDialog *dialog new QDialog(this);dialog->setWindowTitle("切割");dialog->setFixedSize(200, 150);QVBoxLayout *vbox new QVBoxLayout;QHBoxLayout *hbox new QHBoxLayout;QLabel *label new QLabel(&…

BabelDOC,一个专为学术PDF文档设计的翻译和双语对比工具

你是否也有这样的困境&#xff0c;面对一篇学术论文&#xff0c;即使英语水平不错&#xff0c;仍需反复查词典&#xff0c;尤其是遇到专业术语和复杂长句&#xff0c;翻译软件又常常不能很好地处理学术PDF的排版&#xff0c;导致翻译结果混乱不堪。 现在&#xff0c;解决你烦恼…

Python之面向对象和类

一.类1.类的定义&#xff1a;class 类名&#xff1a;“”“注释 ”“”pass2.实例的创建&#xff1a;实例 类名(parameterlist)parameterlist&#xff1a;定义类时__init__()方法的参数&#xff0c;如果该方法只有一个self参数&#xff0c;parameterlist可以省略class Goose()…

【力扣 困难 C】329. 矩阵中的最长递增路径

目录 题目 解法一 题目 待添加 解法一 int max(int a, int b) {return a > b ? a : b; }int search(int** matrix, int m, int n, int i, int j, int (*dp)[n]) {if (dp[i][j]) {return dp[i][j];}int len 0;if (i > 0 && matrix[i - 1][j] > matrix[i]…

Blueprints - UE5的增强输入系统

一些学习笔记归档&#xff1b;增强输入系统由两部分组成&#xff1a;Input Action和Input Mapping ContextInput Action是输入操作的映射&#xff08;操作中比如有移动、跳跃等&#xff09;&#xff0c;Input Mapping Context是输入情境的映射&#xff08;对各种操作的具体按键…

Python 【技术面试题和HR面试题】➕ 动态类型、运算符、输入处理及算法编程问答

1.技术面试题 &#xff08;1&#xff09;TCP与UDP的区别是什么&#xff1f; 答&#xff1a; ①连接性&#xff1a;TCP 面向连接&#xff0c;3次握手及4次挥手&#xff0c;建立端到端的虚链路像&#xff1b;UDP 无连接&#xff0c;直接发送&#xff0c;无需预先建立连接 。 ②传…

etcd-cpp-apiv3 二次封装

接口介绍头文件#include <etcd/Client.hpp> #include <etcd/KeepAlive.hpp> #include <etcd/Response.hpp> #include <etcd/SyncClient.hpp> #include <etcd/Value.hpp> #include <etcd/Watcher.hpp>下面从功能介绍几个类的概念Value &…

【网络与系统安全】强制访问控制——Biba模型

一、模型定义与目标 提出背景&#xff1a;1977年由Ken Biba提出&#xff0c;是首个完整性安全模型&#xff0c;与BLP模型形成对偶&#xff08;BLP关注机密性&#xff0c;Biba关注完整性&#xff09;。核心目标&#xff1a;防止低完整性信息污染高完整性信息&#xff0c;避免未授…

从架构抽象到表达范式:如何正确理解系统架构中的 4C 模型20250704

&#x1f9e9; 从架构抽象到表达范式&#xff1a;如何正确理解系统架构中的 4C 模型&#xff1f; “4C”到底是架构的组成结构&#xff0c;还是架构图的表现方式&#xff1f;这类看似细节的问题&#xff0c;其实直击了我们在系统设计中认知、表达与落地之间的张力。 &#x1f5…

Debian10安装Mysql5.7.44 笔记250707

Debian10安装Mysql5.7.44 笔记250707 1️⃣ 参考 1 在Debian 10 (Buster) 上安装 MySQL 5.7.44 的步骤如下&#xff1a; 1. 添加 MySQL APT 仓库 MySQL 官方提供了包含特定版本的仓库&#xff1a; # 下载仓库配置包 wget https://dev.mysql.com/get/mysql-apt-config_0.8.28…

20250706-6-Docker 快速入门(上)-镜像是什么?_笔记

一、镜像是什么&#xfeff;1. 一个分层存储的文件&#xff0c;不是一个单一的文件分层结构: 与传统ISO文件不同&#xff0c;Docker镜像由多个文件组成&#xff0c;采用分层存储机制存储优势: 每层可独立复用&#xff0c;显著减少磁盘空间占用&#xff0c;例如基础层可被多个镜…

[SystemVerilog] Clocking

SystemVerilog Clocking用法详解 SystemVerilog 的 clocking 块&#xff08;Clocking Block&#xff09;是一种专门用于定义信号时序行为的构造&#xff0c;主要用于验证环境&#xff08;如 UVM&#xff09;中&#xff0c;以精确控制信号的采样和驱动时序。clocking 块通过将信…

kong网关基于header分流灰度发布

kong网关基于header分流灰度发布 在现代微服务架构中&#xff0c;灰度发布&#xff08;Canary Release&#xff09;已经成为一种常用且安全的上线策略。它允许我们将新版本的功能仅暴露给一小部分用户&#xff0c;从而在保证系统稳定性的同时收集反馈、验证效果、规避风险。而作…

Go语言gin框架原理

在gin框架中&#xff0c;最关键的就是前缀树&#xff0c;是很重要的。gin框架本质上是在http包的基础之上&#xff0c;对其的一个二次封装。这里借鉴一下小徐先生的图&#xff0c;可能当前版本的gin可能内容有所改变&#xff0c;但大致思想还是这样。gin框架所做的就是提供一个…