1、判断语句:
if()中可以放方法,如果是与的关系,第一个false后面的方法不执行,或时第一个true也不执行。
2、循环语句:
foreach(int i in Arri[])循环集合或数组。
for()循环也可以循环时间如:
class Program
{
static void Main(string[] args)
{
for (DateTime dt = DateTime.Parse("2010 - 2 - 1"); dt < DateTime.Parse("2010 -10- 1"); dt = dt.AddDays(1))
{
Console.WriteLine(dt);
}
}
}
还可以循环可以累加的类型。
3、跳转语句:
(1)break,跳出全部循环。
(2) Contine 跳出本次循环。
(3)goto 跳转到标志处。
(4)Return 结束当前方法。
(5)throw 抛出异常。
4、异常处理:
(1)try... catch
(2) try... catch finally
(3) try finally。
注意无论怎么样,finally都会执行。
(4)异常类与异常处理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("余额为{0:c}", QuKuan(1000));
Console.ReadLine();
}
catch(Exception Exc)
{
Console.WriteLine(Exc.Message);
Console.ReadLine();
}
}
static double QuKuan(double Money)
{
Console.WriteLine("请输入取款金额:");
int TemMoney = int.Parse(Console.ReadLine());
if (TemMoney > Money)
{
throw new BusException("业务逻辑错误,取款金额大于存款金额!");
}
else
{
return Money - TemMoney;
}
}
}
class BusException : Exception
{
public BusException()
{
}
public BusException(string Message)
: base(Message)
{
File.AppendAllText(@"F:\学习/a.log", Message + "\r\n");
}
}
}
用异常类可以使只有显示层才有输出语句,跨平台处理方便,且写日志时,不用每次有抛出就写,方便。
5、运算符:
(1)与遇0为0。
(2)或遇1为1。
(3)异或全为0。
(4)移位最麻烦头尾补0。
6、表达式:
7、String:
(1)一个String代表一个不可变的顺序字符集。
(2)String一旦定义是不可变的,一旦改变,将生成新的字符串。
(3)字符串若相同,可以共用一个。
8、Console.WriteLing(DateTime.Now.ToString(yyyy年mmm月dd日));中文输出时间。
9、StingBuilder sb=new StringBuilder();
sb.Append("aaaa");
sb.Append("bbbb");
Console.WriteLIne(sb.ToString);
10、Sting与StringBuilder的区别,String为什么用起来跟值类型一样:
因为String一旦定义,字符串就无法改变,如果要改变,就会重新生成一个新的对象,String的不可变性
导致它用起来跟值类型一样,但它确实是引用类型。
String拼接字符串时,会不断的生成新的对象,每拼接一个就会生成一个,导致性能很不好。而StringBuilder
的内部维护着一个数组,如果字符串的长度小于数组长度,会直接放入数组,而不会生成对象,只有在调用ToString
或字符串长度大于数组时才会重新生成新对象,拼接字符串时很方便。
11、编码:
主要有ASCII码,Unicode,万国码。
12、自动售货机::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static double Money = 0;
static void Main(string[] args)
{
Selling Sel1 = new Selling(1, "果粒橙", 3.00);
Selling Sel2 = new Selling(2, "绿茶", 3.00);
Selling Sel3 = new Selling(3, "矿泉水", 1.00);
Selling Sel4 = new Selling(4, "营养快线", 4.00);
Selling Se = new Selling();
while (true)
{
Console.Clear();
Console.WriteLine("******欢迎使用本自动售货机!******");
Console.WriteLine("{0}、{1}, 价格:{2:c}元", Sel1.Count, Sel1.SellName, Sel1.Price);
Console.WriteLine("{0}、{1}, 价格:{2:c}元", Sel2.Count, Sel2.SellName, Sel2.Price);
Console.WriteLine("{0}、{1}, 价格:{2:c}元", Sel3.Count, Sel3.SellName, Sel3.Price);
Console.WriteLine("{0}、{1},价格:{2:c}元", Sel4.Count, Sel4.SellName, Sel4.Price);
Console.WriteLine("5、退出");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("请输入你要买的商品号");
Console.ResetColor();
switch (Console.ReadLine())
{
case "1":
SellGLC();
break;
case "2":
SellGreenTea();
break;
case "3":
SellWater();
break;
case "4":
SellYingYang();
break;
case "5":
Voice("谢谢您的使用,欢迎下次再来!");
Thread.Sleep(4500);
return;
}
}
}
static void SellGLC()
{
Console.WriteLine("您选择购买果粒橙!请投币");
Money = double.Parse(Console.ReadLine());
if (Money < 3)
{
Set();
}
else
{
Console.Beep();
Console.WriteLine("交易成功!找零{0:c}元", Money - 3);
SetVoice();
}
}
static void SellGreenTea()
{
Console.WriteLine("您选择购买绿茶!请投币");
double.TryParse(Console.ReadLine(),out Money);
if (Money < 3)
{
Set();
}
else
{
Console.Beep();
Console.WriteLine("交易成功!找零{0:c}元", Money - 3);
SetVoice();
}
}
static void SellWater()
{
Console.WriteLine("您选择购买矿泉水!请投币");
Money = double.Parse(Console.ReadLine());
if (Money < 1)
{
Set();
}
else
{
Console.Beep();
Console.WriteLine("交易成功!找零{0:c}元", Money - 1);
SetVoice();
}
}
static void SellYingYang()
{
Console.WriteLine("您选择购买营养快线!请投币");
Money = double.Parse(Console.ReadLine());
if (Money < 4)
{
Set();
}
else
{
Console.Beep();
Console.WriteLine("交易成功!找零{0:c}元", Money - 4);
SetVoice();
}
}
static void Set()
{
Console.Beep();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("投入金额太小,请取回您的钱,任意键返回主目录");
Console.ResetColor();
Voice("投入金额太小,请取回您的钱,任意键返回主目录");
Console.ReadKey(true);
}
static void SetVoice()
{
Voice("请拿好您的钱与物品!任意键返回主目录");
Console.ReadKey(true);
}
static void Voice(string i)
{
string content = "CreateObject(\"SAPI.SpVoice\").Speak\"" + i + "\"";
File.WriteAllText(Environment.CurrentDirectory + @"/a.vbs", content, Encoding.Default);
Process.Start(Environment.CurrentDirectory+@"/a.vbs");
}
}
class Selling
{
public int Count;
public string SellName;
public double Price;
public Selling()
{ }
public Selling(int Count, string SellName, double Price)
{
this.Count = Count;
this.SellName = SellName;
this.Price = Price;
}
}
}
转载于:https://blog.51cto.com/benxiaozi/611717