using System;
using System.Collections.Generic;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
//c#语言对表达式的定义:a sequence of one or more operands and zero or more operators can be evaluated to a single value object method namespace
//consist os literal value a method invocation an operator and its operands and simple name
//single value
/*int x;
x = 199;
//object using System.Windows.Forms;
List<int> intList = new List<int>() { 1, 2, 3 };
double[]doubleArray = new double[] { 1.0, 2.0, 3.0 };
var y = intList[1];
Console.WriteLine(x.GetType());*/
//选择语句嵌套
/* int score = 90;
if (score >= 60)
if (score >= 85)
Console.WriteLine("best!");
else
Console.WriteLine("good");
else
Console.WriteLine("failed");*/
// 声明语句:
/* var y = 100;
Console.WriteLine(y.GetType().FullName);
int[] myArray = { 1, 2, 3 };
Console.WriteLine(myArray[2]);
const int x = 100;//常量声明必须跟初始化器*/
//语句分为:标签语句。声明语句,嵌入式语句
//表达式语句
//块语句 语句只能出现在方法体里,
// 块语句是一条完整语句 ctrl +}快捷键在花括号之间跳转,作用域
/*{
hello: Console.WriteLine("hello world");
goto hello;
} */
//选择语句bool 关系表达式是bool类型if 只有一条嵌入语句 代码提示,可以生成代码块
//else语句 多用边界数据测试 else if
/* int score = 60;
if(score >=80 &&score <=100)
{
Console.WriteLine("A");
}
else if (score>=60)
{
Console.WriteLine("B");
}
else if (score>=40)
{
Console.WriteLine("C");
}
else if (score>=0)
{
Console.WriteLine("D");
}*/
/* //switch语句 例子是:成绩判断
Level myLevel = Level.High;
//switch打出来先TAB,再条件里修改myLevel再回车
switch (myLevel)
{
case Level.High:
break;
case Level.Mid:
break;
case Level.Low:
break;
default:
break;
} */
/* Calculator c = new Calculator();
int r = c.Add("100","200");
Console.WriteLine(r);*/
//do语句至少执行一次, while语句可能一次都不执行,如果while判断是false类型
/* int score = 0;
bool canContinue = true;
do
{
Console.WriteLine("please input first number");
string str1 = Console.ReadLine();
int x = 0;
try
{
x = int.Parse(str1);
}
catch
{
Console.WriteLine("First number has problem!restart");
continue;
}
Console.WriteLine("please input second number");
string str2 = Console.ReadLine();
if (str2.ToLower()=="end")
{
break;
}
int y = 0;
try
{
y = int.Parse(str2);
}
catch
{
Console.WriteLine("second number has problem,restart");
continue;
}
int sum = x + y;
if (sum == 100)
{
score++;
Console.WriteLine("Correct!{0}+{1} = {2} ", x, y, sum);
}
else
{
Console.WriteLine("Error!{0}+{1} = {2}", x, y, sum);
canContinue = false;
}
} while (canContinue);
Console.WriteLine("your score is {0}", score);
Console.WriteLine("game over!"); */
//for语句计数循环 tab修改
/*for ( int counter = 0; counter < 10; counter++)
{
Console.WriteLine("hello world");
}
//打印9*9乘法表
for (int a = 1; a <=9; a++)
{
for (int b = 0; b <=9; b++)
{
Console.WriteLine("*");
}
}*/
Calculator c = new Calculator();
int result = c.Add("ABD", "12");
Console.WriteLine(result);
}
/* enum Level
{
High,
Mid,
Low
}*/
//try-catch 对异常值的处理
class Calculator
{
public int Add(string arg1, string arg2)
{
int a = 0;
int b = 0;
try
{
a = int.Parse(arg1);
b = int.Parse(arg2);
}
//通用捕捉异常的语句
catch(ArgumentNullException ane)
{
Console.WriteLine(ane.Message);
Console.WriteLine("your argument are null");
}
//捕捉特定异常的catch语句
catch(FormatException fe)
{
Console.WriteLine(fe.Message);
Console.WriteLine("FORMAT WRONG");
}
catch(OverflowException oe)
{
Console.WriteLine(oe.Message);
Console.WriteLine("out of range!");
}
//释放系统资源 关闭数据库 程序的LOG
finally
{
}
//变量作用阈不会超过声明她的语句块
int result = a + b;
return result;
}
}
}
}
刘铁猛C#语言入门详解——学习笔记014、15、16(2)
最新推荐文章于 2024-04-03 15:46:28 发布