.NET实验一:语言基础

编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。

 using System;

namespace NetExercise1
{
    class Test1
    {
        static void Main(string[] args)
        {
            Console.WriteLine("三角形 or 矩形 ?");
            string choice = Console.ReadLine();
            if (choice.CompareTo("三角形") == 0)
            {
                Console.WriteLine("请输入三角形的三边长度");
                double a = double.Parse(Console.ReadLine());
                double b = double.Parse(Console.ReadLine());
                double c = double.Parse(Console.ReadLine());

                /*
                Console.WriteLine("输入三角形三边长,以空格分隔");
                string value = Console.ReadLine();
                string[] vals = value.Split(' ');
                Console.WriteLine("分开展示各值");
                for (int i = 0; i < vals.Length; i++)
                {
                    Console.WriteLine(string.Format("{0}、{1}", i + 1, vals[i]));
                }
                */
                double length = a + b + c;
                double p = length * 0.5;
                double area = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
                Console.Write("三角形的周长为:");
                Console.WriteLine(length);
                Console.Write("三角形的面积为:");
                Console.WriteLine(area);
            }
            else if(choice.CompareTo("矩形") == 0)
            {
                Console.WriteLine("请输入矩形的长:");
                double l = double.Parse(Console.ReadLine());
                Console.WriteLine("请输入矩形的宽:");
                double w= double.Parse(Console.ReadLine());

                double length2 = (l + w) * 2;
                double area2 = l * w;
                Console.Write("矩形的周长为:");
                Console.WriteLine(length2);
                Console.Write("矩形的面积为:");
                Console.WriteLine(area2);
            }
            else
            {
                Console.WriteLine("输入错误!");
            }
        }
    }
}

编写一个控制台应用程序,可根据输入的月份判断所在季节。

using System;
using System.Collections.Generic;
using System.Text;

namespace NetExercise1
{
    class Test2
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入月份:");
            int month = int.Parse(Console.ReadLine());
            if(month>=3&&month<=5)
            {
                Console.WriteLine(month+"月是春天");
            }
            else if (month >= 6 && month <= 8)
            {
                Console.WriteLine(month + "月是夏天");
            }
            else if (month >= 9 && month <= 11)
            {
                Console.WriteLine(month + "月是秋天");
            }
            else if (month == 12 || month <=2 )
            {
                Console.WriteLine(month + "月是冬天");
            }
        }
    }
}

编写程序,用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至

少有多少个。 

using System;
using System.Collections.Generic;
using System.Text;

namespace NetExercise1
{
    class Test3
    {
        static void Main3(string[] args)
        {

            {
                int sum = 2;
                bool flag = false;
                while (sum < int.MaxValue && flag == false)
                {
                    if (sum % 2 == 1 && sum % 3 == 1 && sum % 4 == 1)
                    {
                        Console.WriteLine("这筐鸡蛋至少有{0}个.", sum);
                        flag = true;
                    }
                    else
                        sum++;
                }
            }
        }
    }   
}

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

using System;
using System.Collections.Generic;
using System.Text;

namespace NetExercise1
{
    class Test4
    {
        static void Main(string[] args)
        {
            List<string> str = new List<string>(); //定义数组
            int length = 0;    //数组长度
            int evensum = 0;  //偶数之和
            int oddsum = 0;  //奇数之和

            //输入数组
            Console.WriteLine("输出数组元素,回车按x结束录入");
            while (true)
            {
                string input = Console.ReadLine();
                if (input.Equals("x") == false)
                    str.Insert(length++, input);
                else
                    break;
            }
            //遍历数组
            for(int i = 0; i < length; i++)
            {
                if(int.Parse(str[i])%2==0)   //string类型转化为int类型
                {
                    evensum = evensum + int.Parse(str[i]);
                }
                else
                {
                    oddsum = oddsum + int.Parse(str[i]);
                }
            }
            Console.WriteLine("奇数之和为:" + oddsum);
            Console.WriteLine("偶数之和为:" + evensum);
        }
    }
}

编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该列上最小。有可能数组没有鞍点)。要求: 

  • u 二维数组的大小、数组元素的值在运行时输入;
  • u 程序有友好的提示信息。
using System;
using System.Collections.Generic;
using System.Text;

namespace NetExercise1
{
    class Test5
    {
        static void Main(string[] args)
        {

            //确定行数和列数
            Console.Write("输入数组行数:");
            int hnum = int.Parse(Console.ReadLine());
            Console.Write("输入数组列数:");
            int lnum = int.Parse(Console.ReadLine());
            int[,] str = new int[hnum, lnum];   //定义二维数组


            //数组赋值
            for (int i = 0; i < hnum; i++)
            {
                Console.WriteLine("输入第" +( i+1 )+ "行");
                for (int j = 0; j < lnum; j++)
                {
                    int num = int.Parse(Console.ReadLine());
                    str[i, j] = num;
                }
            }
            int[] hmax = new int[hnum];   //存储每行的最大值
            int[] lmin = new int[lnum];   //存储每列的最小值
            int jmax = 0;
            //打印数组

            Console.WriteLine("****************数组如下*****************");
            for (int i = 0; i < hnum; i++)
            {
                for (int j = 0; j < lnum; j++)
                {
                    Console.Write(str[i, j] + " ");

                }
                Console.Write("\n");
            }
            //
            for (int i = 0; i < hnum; i++)
            {
                hmax[i] = str[i, 0]; //行的最大值
                for (int j = 0; j < lnum; j++)
                {
                    if (str[i, j] > hmax[i])
                        hmax[i] = str[i, j];
                }
               // Console.WriteLine("第" + (i + 1) + "行的最大值为" + hmax[i]);
            }

            for (int j = 0; j < lnum; j++)
            {
                lmin[j] = str[0, j];  //列的最小值
                for (int i = 0; i < hnum; i++)
                {
                    if (str[i, j] < lmin[j])
                        lmin[j] = str[i, j];
                }
               // Console.WriteLine("第" + (j + 1) + "列的最小值为" + lmin[j]);

            }
            int t = 0;
            for (int i = 0; i < hnum; i++)
            {
                for (int j = 0; j < lnum; j++)
                {
                    if (str[i, j] == lmin[j] && str[i, j] == hmax[i])
                    {
                        Console.WriteLine();
                        Console.WriteLine(str[i, j] + "是鞍点");
                        t++;
                    }
                }
            }
            if(t==0)
            {
                Console.WriteLine();
                Console.WriteLine("此数组没有鞍点!");
            }
        }
    }
}
原文地址:https://www.cnblogs.com/ltw222/p/15542873.html