try-catch
数字加倍:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
int number = 0;
bool is_flag = true;
Console.WriteLine("请输入数字:");
try
{
number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的数字不能转化!");
is_flag = false;
}
if (is_flag)
{
Console.WriteLine("加倍后:{0}", number*2);
}
Console.ReadKey();
}
}
}
switch-case:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
bool is_flag = true;
Console.WriteLine("input:");
string level = Console.ReadLine();
double salary = 500;
switch (level)
{
case "a": salary += 300;
break;
case "b": salary += 200;
break;
case "c": salary += 100;
break;
default: Console.WriteLine("输入错误!");
is_flag = false;
break;
}
if (is_flag)
{
Console.WriteLine("翻倍工资:{0}", salary);
}
Console.ReadKey();
}
}
}
使用switch-case的定值判定分数等级:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
bool is_flag = true;
int level = 0;
Console.WriteLine("Input Score:");
try
{
level = Convert.ToInt32(Console.ReadLine())/10;
}
catch
{
is_flag = false;
Console.WriteLine("输入错误!");
}
switch (level)
{
case 10:
case 9: Console.WriteLine("Rank:A");
break;
case 8: Console.WriteLine("Rank:B");
break;
case 7: Console.WriteLine("Rank:C");
break;
default: Console.WriteLine("Rank:D");
break;
}
if (!is_flag)
{
Console.WriteLine("Rank:None");
}
Console.ReadKey();
}
}
}
判断月份天数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入年份:");
int year = Convert.ToInt32(Console.ReadLine());
try
{
Console.WriteLine("请输入月份:");
int month = Convert.ToInt32(Console.ReadLine());
int day = 0;
if (month >= 1 && month <= 12)
{
switch (month)
{
case 4:
case 6:
case 9:
case 11: day = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 29;
}
else
{
day = 28;
}
break;
default: day = 31;
break;
}
Console.WriteLine("{0}年{1}月,有{2}天。", year, month, day);
}
else
{
Console.WriteLine("月份越界!");
}
}
catch
{
Console.WriteLine("输入的月份有误,程序退出!");
}
}
catch
{
Console.WriteLine("输入的年份有误,程序退出!");
}
Console.ReadKey();
}
}
}
断点调试
- 单步运行调试F11,黄色的是即将执行的代码,
- 逐过程调试 F10,
- 设置观察变量,栏中写入变量或者表达式,
- 设置断点