if判断语句:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace wm110_2
{
class Program
{
static void Main(string[] args)
{
Console.Write("输入任意的一个数字、字母:\nx="); //提示
char x = Convert.ToChar(Console.ReadLine()); //用来保存输入的字符
if (Char.IsUpper(x))
{
Console.WriteLine("大写字母");
}
else if (Char.IsLower(x))
{
Console.WriteLine("小写字母");
}
else if (Char.IsDigit(x))
{
Console.WriteLine("数字");
}
else
{
Console.WriteLine("未知");
}
}
}
}
运行结果:
输入任意的一个数字、字母:
x=&
未知
请按任意键继续. . .
在case语句里面使用goto:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace wm110_3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输出结果为:\n选对号码:1=小 2=大 3=很大");
Console.Write("请输入你的选择:");
string s = Console.ReadLine();
int n = int.Parse(s); //将字符串转化为整形
int cost = 0;
switch (n)
{
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
case 3:
cost += 50;
goto case 1;
default:
Console.WriteLine("请安要求输入!!!");
break;
}
if (cost != 0)
{
Console.WriteLine("应付费用{0}元",cost);
}
}
}
}
运行结果:
输出结果为:
选对号码:1=小 2=大 3=很大
请输入你的选择:3
应付费用75元
请按任意键继续. . .
foreach语句的使用:
foreach语句是为了简化对数组或集合的循环访问而设计的。
格式如下:
foreach(类型 变量名 in 集合对象)
{
//语句体
}
示例:利用foreach输出一个数组里面的全部内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace wm110_4
{
class Program
{
static void Main(string[] args)
{
int[] Array = { 0, 2, 4, 6, 8, 10 }; //初始化一个整形数组
Console.WriteLine("输出数组里面的每一个元素:");
foreach (int temp in Array)
{
Console.Write(temp.ToString() + " "); //输出数组中每一个元素的值
}
Console.WriteLine();
}
}
}
运行结果:
输出数组里面的每一个元素:
0 2 4 6 8 10
请按任意键继续. . .