.Net实验一 语言基础

发布时间 2023-12-02 16:28:29作者: YE-

一、实验目的

  1. 熟悉Visual Stido.NET 实验环境;
  2. 掌握控制台程序的编写方法;
  3. 掌握C#程序设计语言的语法基础;
  4. 掌握控制语句和数组的使用。
    二、实验要求
    根据题目要求,编写 C#程序,并将程序代码和运行结果写入实验报告。
    三、实验内容
  5. 编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。
using System;
namespace Text
{
    class Program
    {
        static void Main(string[] args)
        {
            float L;
            double S;
            float a, b, c, z, y;
            Console.WriteLine("请选择计算三角形或者长方形,三角形输入1,长方形输入2:");
            char x = char.Parse(Console.ReadLine());
            switch (x)
            {
                case '1':
                L1: Console.WriteLine("请输入三角形的三边长");
                    Console.WriteLine("请输入三角形的a边:");
                    a = float.Parse(Console.ReadLine());
                    Console.WriteLine("请输入三角形的b边:");
                    b = float.Parse(Console.ReadLine());
                    Console.WriteLine("请输入三角形的c边:");
                    c = float.Parse(Console.ReadLine());
                    if (a + b <= c || a + c <= b || b + c <= a)
                    {
                        Console.WriteLine("该三角形不成立,请重新输入:");
                        goto L1;
                    }
                    else
                    {
                        L = (a + b + c) / 2;
                        S = Math.Sqrt(L * (L - a) * (L - b) * (L - c));
                        Console.WriteLine("该三角形周长为:{0}\n面积为:{1}", 2 * L, S);
                    }
                    break;
                case '2':
                    Console.WriteLine("请分别输入长方形的长和宽:");
                    z = float.Parse(Console.ReadLine());
                    y = float.Parse(Console.ReadLine());
                    L = (z + y) * 2;
                    S = z * y;
                    Console.WriteLine("该长方形周长为:{0}\n面积为:{1}", L, S);
                    break;
                default:
                    Console.WriteLine("输入错误,请重新选择");
                    break;
            }
            Console.ReadKey();
        }
    }
}
  1. 编写一个控制台应用程序,可根据输入的月份判断所在季节。
using System;
namespace HelloWorldApplication
{
    /* 类名为 HelloWorld */
    class HelloWorld
    {
        /* main函数 */
        static void Main(string[] args)
        {
            Console.WriteLine("您好,请输入月份:");
            int Month = Console.Read() - 48;
            
            String Season;
            if (3 <= Month && Month <= 5) {
                Season = "春";
            } else if (6 <= Month && Month <= 8)
            {
                Season = "夏";
            }
            else if (9 <= Month && Month <= 11)
            {
                Season = "秋";
            }
            else
            {
                Season = "冬";
            }
            Console.WriteLine("您好,{0}月是{1}季节", Month, Season);
            Console.ReadKey();
        }
    }
}
  1. 编写程序,用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两
    个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至
    少有多少个。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num = 0;

        while (num < 100) {
            num++;
            if ((num % 2 == 1) && (num % 3 == 1) && (num % 4 == 1) && (num != 0) && (num != 1)) {
                break;
            }
            
        }
        Console.WriteLine("至少有鸡蛋:" + num + "个!");
        Console.ReadKey();
    }
}

}
4. 编写程序,计算数组中奇数之和和偶数之和。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 1,2,3,4,5,6,7,8,9};
            int ji_sum = 0;
            int ou_sum = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (Program.IsSuNum(a[i]))
                {
                    ou_sum = ou_sum + a[i];
                }
                else
                {
                    ji_sum = ji_sum + a[i];
                }
            }
            Console.WriteLine("奇数的和:" + ji_sum + "偶数的和:" + ou_sum);
            Console.ReadKey();
        }
        public static bool IsSuNum(int a)
        {
            int i = 1;
            if (a % 2 == 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }

}
  1. 编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该
    列上最小。有可能数组没有鞍点)。要求:
      二维数组的大小、数组元素的值在运行时输入;
      程序有友好的提示信息。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            int n, m;
            Console.WriteLine("请输入n,m(n * m大小的二维数组):");
            n = Convert.ToInt32(Console.ReadLine());
            m = Convert.ToInt32(Console.ReadLine());
            int[,] a = new int[n, m];
            Console.WriteLine("请输入每个值:\n");
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    Console.WriteLine("a[" + i + "][" + j + "]:");
                    a[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            for (int i = 0; i < n; i++)
            {
                int max = a[i, 0];
                int lie = 0;
                for (int j = 0; j < m; j++)
                {
                    if (max < a[i, j])
                    {
                        max = a[i, j];
                        lie = j;
                    }
                }
                int min = max;
                for (int f = 0; f < n; f++)
                {

                    if (min > a[f, lie])
                    {
                        min = a[f, lie];
                    }
                }
                if (min == max)
                {
                    Console.WriteLine("鞍点:" + "n=" + i + ",m=" + lie + ",value=" + a[i, lie]);
                    Console.ReadKey();
                }
            }
        }
        public static bool IsBigSmole(int[] a)
        {
            return true;
        }


    }

}