C#语法

1 篇文章 0 订阅
1 篇文章 0 订阅

选择结构

if结构
语法:
1.if(条件表达式)
{
代码块
}

2.if(条件表达式)
{
代码块1
}
else
{
代码块2
}
3.if(条件表达式1)
{
代码块1
}
else if(条件表达式2)
{
代码块2
}
else
{
代码块3
}
4.if(条件表达式1)
{
.if(条件表达式2)
{
代码块1
}
else
{
代码块2
}
}
else
{
代码块3
}
switch结构
switch(表达式){
case 常量表达式 1 :
语句1
break; 必须有
case 常量表达式 2 :
语句2
continue; 必须有
default : 可选的
语句n
break; 必须
}
例子1.

namespace BankName
{
    /// <summary>
    /// 通过银行简称输入银行全称
    /// </summary>
     class FullBankName
    {

        public void BnakNameOutPut()
        {
            Console.WriteLine("请输入银行简称:");
            string bankJc = Console.ReadLine();
            switch (bankJc)
            {
                case "ICBC":
                    Console.WriteLine("中国工商银行");
                    break;
                case "CBC":
                    Console.WriteLine("建行");
                    break;
                case "ABC":
                    Console.WriteLine("农行");
                    break;
                default:
                    Console.WriteLine("输入有误");
                    break;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FullBankName bankName = new FullBankName();
            bankName.BnakNameOutPut();
            Console.ReadLine();
        }
    }
}

例子2

 static void Main(string[] args)
        {
            Console.WriteLine("请输入今天是星期几:");
            string week = Console.ReadLine();

            switch (week)
            {
                case "一":
                case "二":
                case "三":
                    Console.WriteLine("上C#课");
                    break;
                case "四":
                case "五":
                    Console.WriteLine("上sql server课");
                    break;
                case "六":
                case "日":
                    Console.WriteLine("放假");
                    break;
            }
            Console.ReadLine();
        }

数组与循环

一维数组
语法:

  • 数据类型【】数组名
  • 数据类型 数组名【】
  • 例如 int [] array;
    获取数组长度的方法
    数组名.Length
    访问数组中的元素
    数组名[下标] 数组的下标从0开始
    例子3
class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[5] { 0, 1, 2, 3, 4 };//创建一个含有5个整型数字的数组

            int[] array1 = new int[] { 0, 1, 2, 3, 4 };//创建一个长度不确定的数组
            int[] array2 = { 0, 1, 2, 3, 4 }; //直接为数组赋值
            Console.WriteLine("数组array的长度:"+array.Length);
            Console.WriteLine("数组array1的长度:"+array1.Length);
            Console.WriteLine("数组array2的长度:"+array2.Length);

        }
    }

例子4 对象数组

namespace StudentArray
{
    public class Student
    {
        public string Name { get; set; }//属性
        public double Score { get; set; }
        public void ShopInfo()//方法
        {
            Console.WriteLine(Name+"\t"+Score);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //定义对象数组
            Student[] stus = new Student[3];
            stus[0] = new Student();
            stus[0].Name = "张三";
            stus[0].Score = 60;
            stus[1] = new Student();
            stus[1].Name = "李四";
            stus[1].Score = 80.5;
            stus[2] = new Student();
            stus[2].Name = "王五";
            stus[2].Score = 90.5;
            Console.WriteLine("对象数组中的学生成绩信息");
            foreach (Student item in stus)
            {
                item.ShopInfo();
            }
            Console.ReadLine();
        }
    }
}

循环结构

while循环
语法
while(条件表达式)
{
代码块
}
do-while循环
语法
do
{
//代码块
}while(条件表达式)
for循环
for(表达式1;表达式2;表达式3)
{
代码块
}
例子5

 class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[5] { 0, 1, 2, 3, 4 };
            Console.WriteLine("数组array中{0}个数据的值是:",array.Length);
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i]+"\t");
            }
            Console.ReadLine();
        }
    }

foreach循环
foreach(元素类型 元素变量名 in 数组)
{
代码块
}
例子6

class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[5] { 0, 1, 2, 3, 4 };
            Console.WriteLine("数组array中{0}个数据的值是:", array.Length);
            foreach (int item in array)
            {
                Console.Write(item + "  ");
            }
            Console.ReadLine();
        }
    }

例子7

 Console.WriteLine("请输入一句话:");
            string line = Console.ReadLine();
            foreach (char item in line)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();

二重循环

例子8

			int i, j;
            for ( i = 0; i <=5; i++)
            {
                for ( j = 0; j <=i; j++)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }

冒泡排序:数字排序的一种方式,即比较大小,排名次
口诀(升序)

  • N个数字来排队,两两相比小靠前
  • 外层循环N-1,内层循环N-1-i
  • 如果要降序,把程序中的大于号改为“<”

例子9

    class Program
    {
        static void Main(string[] args)
        {
            int[] scores = new int[5];//存储成绩的数组
            int i, j;//循环变量
            int temp;//临时变量

            Console.WriteLine("请输入5个选手的成绩:");
            for ( i = 0; i < scores.Length; i++)
            {
                Console.WriteLine("请输入第{0}个学生的成绩:",i+1);
                scores[i] = Convert.ToInt32(Console.ReadLine());
            }
            //使用冒泡排序
            for ( i = 0; i < scores.Length-1; i++)//控制比较多少轮
            {
                for ( j = 0; j < scores.Length-1-i; j++)
                {
                    if (scores[j] < scores[j + 1])
                    {
                        temp = scores[j];
                        scores[j] = scores[j + 1];
                        scores[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine("排序后的成绩为:");
            foreach (int item in scores)
            {
                Console.WriteLine(item+"\t");
            }
            Console.ReadLine();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值