分享几个上机案例题

1.从控制台中输入一个数,如果是1,输出壹;如果是2,输出贰,如果是三,输出叁,否则输出没有

static void Main(string [] args)
{
    Console.WriteLine("请输入一个数");
    int a=int.Parse(Console.ReadLine());
    test1(a);
}
static void test1(int i)
{
  switch(i)
  {
        case1:
          Console.WriteLine("壹");
        break;
        case2:
          Console.WriteLine("贰");
        break;
        case3:
          Console.WriteLine("叁");
        break;
        default:
          Console.WriteLine("没有");
        break;
  }
}


2.从控制台中输入一组数据,放在数组中,使用for循环遍历输出

static void Main(string [] args)
{
    Console.WriteLine("指定数据长度:");
    int length=int.Parse(Console.ReadLine());
    int [] i=new int[length];
    test2(length,i)
}
static void test2(int length,int[]array)
{
   for(int i=0;i<Length;i++)
   {
       Console.WriteLine("请输入:");
       array[i]=int.Parse(Console.ReadLine());
   }
   Console.WriteLine("输出:");
   for(int i=0;i<=Length-1;i++)
   {
       Console.WriteLine(array[i]+",");
   }
}

3.冒泡排序将一组数据从小到大输出

static void Main(string [] args)
{
    int [] i=new int[]{1,3,1,4,5,2,0};
    test3(i);
}
static void test3(int []array)
{
    int temp;
    for(int i=0;i<array.Length-1;i++)
    {
        for(int j=0;j<array.Length-1-i;j++)
        {
            if(array[j]>array[j+1])
            {
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
        }
    }
}

4.循环输出1-200之间,所有三的倍数之和

static void Main(string [] args)
{
    test4(1,200);
}
static void test4(int start,end)
{
    for(start;start<=end;start++)
    {
        if(start%3==0)
        {
            Console.WriteLine(start);
        }
    }
}

5.使用do-while循环输出1-100之间所有整数之和

static void Main(String [] args)
{
    test5(1,100);
}
static void test5(int sta,int end)
{
    int sum=0;
    do
    {
        sum=sum+sta;
        sta++;
    }while(sta<=end);
    Console.WriteLine(sum);
}

6.从控制台中输入5个学生的名字,使用foreach遍历

static void Main(String [] args)
{
    string [] str=new string[5];
    test6(str);
}
static void test6(string [] str)
{
    for(int i=0;i<str.Length;i++)
    {
        Console.WriteLine("请输入学生名:")
        str[i]=Console.ReadLine();
    }
    foreach(string s in str)
    {
        Console.WriteLine(s);
    }
}

7.创建一个类Car,其中包含两个属性,name和color,封装两个属性,规定颜色只有银色,黑色和白色,其他默认黑色

class Car
{
    private string name;
    public string Name
    {
        get{return this.name;}
        set{this.name=value;}
    }
    
    private char color;
    public char GetColor()
    {
        return color;
    }
    public void SetColor(char color)
    {
        if(color.Equals('银')||color.Equals('黑')||color.Equals('白'))
        {
            this.color=color;
        }else
        {
            color='黑';
        }
    }
}

8.输出99乘法表

static void Main(String [] args)
{
    test8();
}
static void test8 ()
{
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=i;j++)
        {   
            Console.Write(i+"*"+j+"="+(i*j)+"\t");
        }
        Console.WriteLine();
    }
}

9.声明一个有参有返回值的方法,并且在main方法中调用

static void Main(string [] args)
{
    int i=test9(9);
}
static int test9(int i)
{
    i++;
    return i;
}

10.有一个字符串“我爱你中国”   输出字符串的长度,输出爱的位置,如何截取中国两字,输出格式为:“我,爱,你,china”

static void Main(string [] args)
{
  string str="我爱你中国";
    
    Console.WriteLine(str.Length);
    int index = str.IndexOf("爱");
    Console.WriteLine(index);
    string china;
    //只有一个参数的时候,默认截取至最后
    china=str.Substring(str.IndexOf("中"));
    
    //输出“我,爱,你,china”
    test10(str);
}
static void test10(string str)
{
    for(int i=1;i<=6;i+=2)
    {
        str=str.Insert(i,",");
    }
  str=str.Replace("中国","china");
    Console.WriteLine(str);
}


往期精彩

这……

2021-04-10

今晚在学校值班……

2021-04-09

3班的第二次模拟面试

2021-04-08

我们在进行着一场拔河比赛……

2021-04-07

asp.net中条件查询+分页

2021-04-06

表单提交报错405的解决方式

2021-04-04

点分享

点点赞

点在看

posted @ 2021-04-11 06:00  穆雄雄  阅读(52)  评论(0编辑  收藏  举报