C#作业--作业一

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

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

namespace 作业1_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i; double t_1, t_2, t_3; double r_lenth, r_width; double cir, area;                        //三角形及长方形的变量等
            Console.WriteLine("作业1-1");
            while (true)
            {
                Console.WriteLine("       1.三角形     2.长方形    3.退出          ");
                Console.WriteLine("请输入序号:");
                i = int.Parse(Console.ReadLine());
                switch (i)
                {
                    case 1:                                     //三角形周长及面积算法
                        Console.WriteLine("请输入第一条边长:");//用户依次输入三个边长
                        t_1 = float.Parse(Console.ReadLine());
                        Console.WriteLine("请输入第二条边长:");
                        t_2 = float.Parse(Console.ReadLine());
                        Console.WriteLine("请输入第三条边长:");
                        t_3 = float.Parse(Console.ReadLine());//输入结束
                        cir = t_1 + t_2 + t_3;                //计算三角形周长及面积
                        area = Math.Sqrt(cir / 2 * (cir / 2 - t_1) * (cir / 2 - t_2) * (cir / 2 - t_3));//无法将double隐式转换为float,之前用的float类型的边长等,sqrt函数返回为double
                        Console.WriteLine("该三角形周长为:{0}", cir);
                        Console.WriteLine("该三角形面积为:{0}", area);
                        break;
                    case 2:                                    //长方形周长及面积算法
                        Console.WriteLine("请输入长方形的长:");//用户依次输入三个边长
                        r_lenth = float.Parse(Console.ReadLine());
                        Console.WriteLine("请输入长方形的宽:");
                        r_width = float.Parse(Console.ReadLine());
                        cir = 2 * (r_lenth + r_width);        //长方形周长及面积
                        area = r_lenth * r_width;
                        Console.WriteLine("该长方形周长为:{0}", cir);
                        Console.WriteLine("该长方形面积为:{0}", area);
                        break;
                    case 3: Console.WriteLine("欢迎您的下次进入!!!"); break;
                    default: Console.WriteLine("输入错误!!!"); break;
                }//switch结束
                if (i == 3) break; //判断用户是否退出系统
            }//while循环结束
        }
    }
}


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

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

namespace _1_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("**********作业1-2***************");
            while (true)
            {
                Console.WriteLine("请输入月份:");
                i = int.Parse(Console.ReadLine());
                switch (i)
                {
                    case 3:
                    case 4:
                    case 5: Console.WriteLine("春季"); break;
                    case 6:
                    case 7:
                    case 8: Console.WriteLine("夏季"); break;
                    case 9:
                    case 10:
                    case 11: Console.WriteLine("秋季"); break;
                    case 12:
                    case 1:
                    case 2: Console.WriteLine("冬季"); break;
                    default: Console.WriteLine("输入错误!!!"); break;
                }//switch结束
            }//while结束
        }
    }
}
View Code


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

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

namespace _1_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 2;                 //鸡蛋个数总量
            bool flag = false;
            while (sum < int.MaxValue && flag == false)
            {  //循环条件:鸡蛋总量没有超出int所表示最大值,且没找到一个符合条件的
                if (sum % 2 == 1 && sum % 3 == 1 && sum % 4 == 1) //满足题目条件,已找到
                {
                    Console.Write("这篮鸡蛋至少有{0}", sum);
                    Console.WriteLine("个。");
                    flag = true;
                }
                else                                            //没找到,增加鸡蛋数量
                    sum++;
            }//while循环结束
        }
    }
}
View Code


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

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

namespace _1_4
{
    class Program
    {
        static void Main(string[] args)
        {
            int total = 0,total2=0;//total用来记录arr数组元素的偶数的和
            int[] arr = new int[] { 1,2,3,4,5,6,7 };//静态初始化一维数组
            foreach (int x in arr)//从数组arr中提取整数
            {
                if (x % 2 == 0)
                { total += x; }
                else {
                    total2 += x;
                }
            }
            Console.WriteLine("数组中偶数之和为:{0}", total);
            Console.WriteLine("数组中奇数之和为:{0}", total2);
        }
    }
}
View Code


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

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

namespace _1_5
{
    class Program
    {
        static void Main(string[] args)
        {
            while(true)
            {
                Console.WriteLine("请输入二维数组的行数:");
                int row = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入二维数组的列数:");
                int column = int.Parse(Console.ReadLine());
                float[,] a = new float[row, column];        //创建该二维数组
                Console.WriteLine("请输入二维数组元素:");
                for (int i = 0; i < row; i++)               //每行循环
                {
                    for (int j = 0; j < column; j++)        //每列循环
                    {
                        Console.WriteLine("请输入第{0}个数:", i * row + j + 1);
                        a[i, j] = float.Parse(Console.ReadLine());
                    }
                }
                int count = 0;                             //计数器
                for (int i = 0; i < row; i++)            //行循环
                {
                    int maxj = 0;                        //初始化该行最大的值所在列
                    for (int j = 0; j < column; j++)     //查找该行最大值的所在列
                    {
                        if (a[i, j] > a[i, maxj])
                        {
                            maxj = j;                    //把每行中最大的列号赋给maxj
                        }
                    }//找到该行最大数
                    int minx = 0;                       //初始化该行最大的值所在列中最小的值所在行
                    for (int j = 0; j < row; j++)       //查找该行最大值的所在列中该列的最小值
                    {
                        if (a[j, maxj] < a[minx, maxj])
                            minx = j;
                    }//找到该行最大数所在列的最小值
                    if (a[i, maxj] == a[minx, maxj])//判断该行最大数所在列的最小值的行号是否为该行
                    { Console.Write("鞍点[{0},{1}]:{2}" + '\n', minx, maxj, a[minx, maxj]); count++; }
                }//行循环结束
                if (count == 0) Console.WriteLine("没有鞍点数");
                else
                {
                    Console.WriteLine("鞍点总数为:" + count);
                };
               
            }//while循环结束

        }
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/miria-486/p/10072733.html