C# 1,数字查重,并返回数字序号 2,求4行5列矩阵中的最大值的行列数 3,使用while语句求用户输入的数字区间的奇数和

程序包含功能:
1.定义一个内容为10的整数数组(无重复),内容自拟,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”
2.求4行5列矩阵中的最大值的行列数
3.使用while语句求用户输入的数字区间的奇数和
要求:
用户选择指定功能进行实现,例如:用户键入2,选择求最大值行列功能,那么只实现该功能,实现后返回选择功能界面,按特定键(某一个按键比如“q”)。

主函数的方法调用:因为需要做到方法选择循环,我第一个想到的是用for循环嵌套swich case 进行选择循环,形式如下:

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

namespace 方法的循环选择测试
{
    class Program
    {
        static void Main(string[] args)
        {
            for (; ; )//因为在C#中for循环可以空,这样就实现了无限的循环
            {
                Console.WriteLine("1,方法一");
                Console.WriteLine("2,方法二");
                Console.WriteLine("3,方法三");
                Console.WriteLine("按0退出");
                Console.WriteLine("请告诉我你的选择:");//以上是给用户的选择提示
                int XZ=Convert.ToInt32(Console.ReadLine());//设置选择变量,通过变量,告诉程序用户的选择
                if (XZ == 0)
                    break;//这个break是用来结束for循环,因为空的for循环为无限循环,所以需要一个条件来结束循环;
                else
                switch(XZ)
                {
                    case 1:
                        NUM1();//方法调用,case与break之间可以写多行代码,并非只能一步调用
                        break;
                    case 2:
                        NUM2();
                        break;
                    case 3:
                        NUM3();
                        break;
                    default :
                        Console.WriteLine("请选择正确的序列号!");
                        break;
                }
                Console.WriteLine();//这步空行是为了分隔两次选择;
            }
            
        }
        static void NUM1()//调用函数
        {
            Console.WriteLine("我是方法一!");
        }
        static void NUM2()
        {
            Console.WriteLine("我是方法二!");
        }
        static void NUM3()
        {
            Console.WriteLine("我是方法三");
        }
    }
}

1.定义一个内容为10的整数数组(无重复),内容自拟,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”,代码如下:

 static public int FindNum(int a)//1.定义一个内容为10的整数数组(无重复),内容自拟,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”
        {
            int[] arry = new int[10] { 0, 1, 5, 4, 98, 65, 48, 75, 3, 48 };//此数组为程序内置数组
            for (int i = 0; i < arry.Length; i++)
            {
                if (arry[i] == a)//判断是否相同,若相同则返回该位置;
                    return i + 1;//因为C#数组角标从0开始,所以返回的位置需要加1;
            }
            return -1;//若没有匹配项则返回-1,表示不存在;

        } 

2.求4行5列矩阵中的最大值的行列数,代码段如下:

  static public int[] Max(int[,] a)//2.求4行5列矩阵中的最大值的行列数
        {
            int max = a[0, 0];//初步设置最大值为数组的第一个位置的数字,后面比较中再进行修改
            int x = 0, y = 0;//x用来记录行数,y用来记录列数;
            for (int i = 0; i < a.GetLength(0); i++)//遍历行
            {
                for (int j = 0; j < a.GetLength(1); j++)//遍历列
                {
                    if (a[i, j] > max)//判断最大值
                    {
                        max = a[i, j];//若if为真,则修改最大值,并记录其行列数
                        x = i + 1;
                        y = j + 1;
                    }
                }

            }
            int[] b = new int[3] { max, x, y };//因为所求结果需要返回三个值,最简单的方法就是把这三个数付给数组,并把数组返回;
            return b;
        } 

3.使用while语句求用户输入的数字区间的奇数和(因为为了练习才用的while,用for更简单),代码段如下:

    static public int Add(int x, int y)//3.使用while语句求用户输入的数字区间的奇数和
        {
            int add = 0;//设置累加和
            while (x < y)
            {
                if (x % 2 != 0)//x对2取余数,若余数不为0,则为奇数
                    add += x;//奇数累加
                x += 1;//因为x与y之间的每个数都要判断,故对当前x进行操作需要加1;
            }
            return add;//返回累加和;
            //还有一个思路,我没写:先判断初始x是否为奇数,若是奇数则累加,并对x加2,因为初始的x若为奇数,则其加2也是奇数,若初始x是偶数,则加1,然后累加,之后的x在假2,在累加;(可能我说的不清楚,理解万岁)
        } 

下面是这个小程序的完整代码,没怎么写注释,理解这看看吧,很简单的:

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

namespace _17第二期暑假作业1
{
    class Program
    {
        static void Main(string[] args)
        {//函数调用以下方法:
            //1.定义一个内容为10的整数数组(无重复),内容自拟,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”
            //2.求4行5列矩阵中的最大值的行列数
            //3.使用while语句求用户输入的数字区间的奇数和
            int RW;
            for (; ; )
            {
                Console.WriteLine("1.有十个数放入数组中,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”");
                Console.WriteLine("2.求4行5列矩阵中的最大值的行列数");
                Console.WriteLine("3.使用while语句求用户输入的数字区间的奇数和");
                Console.WriteLine("按“0”退出程序");
                Console.WriteLine("请选择任务:");
                RW =Convert.ToInt32(Console.ReadLine());
                if (RW == 0)
                    break;
                else
                {
                    switch (RW)
                    {
                        case 1://1.定义一个内容为10的整数数组(无重复),内容自拟,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”
                            Console.WriteLine("请输入要校验的数字");
                            int a = Convert.ToInt32(Console.ReadLine());
                            int  T = FindNum(a);
                            if (T >=0)
                                Console.WriteLine("{0}在第{1}个位置",a,T);
                            else
                                Console.WriteLine("不存在");
                            break;
                        case 2://2.求4行5列矩阵中的最大值的行列数
                            int[,] arry=new int[4,5];
                            Console.WriteLine("请输入整数数组内容:");
                            for (int i = 0; i < arry.GetLength(0);i++ )
                            {
                                for (int j = 0; j < arry.GetLength(1); j++)
                                {
                                    int b = Convert.ToInt32(Console.ReadLine());
                                    arry[i, j] = b;
                                }
                            }
                            int[] brry = Max(arry);
                            Console.WriteLine("最大值是"+brry[0]+"处于第"+brry[1]+"行,第"+brry[2]+"列");
                                break;
                        case 3://3.使用while语句求用户输入的数字区间的奇数和
                                Console.WriteLine("请输入数字区间下限:");
                                int x = Convert.ToInt32(Console.ReadLine());
                                Console.WriteLine("请输入区间上限:");
                                int y = Convert.ToInt32(Console.ReadLine());
                                int add = Add(x, y);
                                Console.WriteLine("区间内奇数和为:"+add);
                            break;
                        default:
                            Console.WriteLine("输入错误!请重新输入!");
                            break;

                    }
                }
                Console.WriteLine();
            }
        }
        #region 数字匹配
        static public int FindNum(int a)//1.定义一个内容为10的整数数组(无重复),内容自拟,用户输入一个数值,找出这个数值在本数值中的序号,如果没有输出“不存在”
        {
            int[] arry = new int[10] { 0, 1, 5, 4, 98, 65, 48, 75, 3, 48 };
            for (int i = 0; i < arry.Length; i++)
            {
                if (arry[i] == a)
                    return i + 1;
            }
            return -1;

        } 
        #endregion
        #region 最大值
        static public int[] Max(int[,] a)//2.求4行5列矩阵中的最大值的行列数
        {
            int max = a[0, 0];//初步设置最大值为数组的第一个位置的数字,后面比较中再进行修改
            int x = 0, y = 0;//x用来记录行数,y用来记录列数;
            for (int i = 0; i < a.GetLength(0); i++)//遍历行
            {
                for (int j = 0; j < a.GetLength(1); j++)//遍历列
                {
                    if (a[i, j] > max)//判断最大值
                    {
                        max = a[i, j];//若if为真,则修改最大值,并记录其行列数
                        x = i + 1;
                        y = j + 1;
                    }
                }

            }
            int[] b = new int[3] { max, x, y };//因为所求结果需要返回三个值,最简单的方法就是把这三个数付给数组,并把数组返回;
            return b;
        } 
        #endregion
        #region 奇数和
        static public int Add(int x, int y)//3.使用while语句求用户输入的数字区间的奇数和
        {
            int add = 0;//设置累加和
            while (x < y)
            {
                if (x % 2 != 0)//x对2取余数,若余数不为0,则为奇数
                    add += x;//奇数累加
                x += 1;//因为x与y之间的每个数都要判断,故对当前x进行操作需要加1;
            }
            return add;//返回累加和;
            //还有一个思路,我没写:先判断初始x是否为奇数,若是奇数则累加,并对x加2,因为初始的x若为奇数,则其加2也是奇数,若初始x是偶数,则加1,然后累加,之后的x在假2,在累加;(可能我说的不清楚,理解万岁)
        } 
        #endregion

    }
}

猜你喜欢

转载自blog.csdn.net/qq_40979936/article/details/81189581