方法练习

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

namespace _15方法练习
{
    class Program
    {
        static void Main(string[] args)
        {
            // 提示用户输入两个数字  计算这两个数字之间所有整数的和
            //1、用户只能输入数字
            //2、计算两个数字之间和
            //3、要求第一个数字必须比第二个数字小  就重新输入
            Console.WriteLine("请输入第一个数字");
            string strNumberOne = Console.ReadLine();
            int numberOne = GetNumber(strNumberOne);
            Console.WriteLine("请输入第二个数字");
            string strNumberTwo = Console.ReadLine();
            int numberTwo = GetNumber(strNumberTwo);

            //判断第一个数字是否小于第二个数字
            JudgeNumber(ref numberOne, ref numberTwo);

            //求和
            int sum = GetSum(numberOne, numberTwo);
            Console.WriteLine(sum);
            Console.ReadKey();


        }


        public static void JudgeNumber(ref int n1, ref int n2)
        {
            while (true)
            {
                if (n1 < n2)
                {
                    //复合题意
                    return;
                }
                else//>=2
                {
                    Console.WriteLine("第一个数字不能大于或者等于第二个数字,请重新输入第一个数字");
                    string s1 = Console.ReadLine();
                    //调用GetNumber
                    n1 = GetNumber(s1);
                    Console.WriteLine("请重新输入第二个数字");
                    string s2 = Console.ReadLine();
                    n2 = GetNumber(s2);
                }

            }

        }
        public static int GetNumber(string s)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(s);
                    return number;
                }
                catch
                {
                    Console.WriteLine("输入有误!!!请重新输入");
                    s = Console.ReadLine();

                }
            }
        }

        public static int GetSum(int n1, int n2)
        {
            int sum = 0;
            for (int i = n1; i <= n2; i++)
            {
                sum += i;
            }
            return sum;
        // return作用
        //1、在方法中返回要返回的值。
        //2、立即结束本次方法。
} } }

 练习二

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

namespace _01方法的练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //79、用方法来实现:有一个字符串数组:
            //{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最
            //string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            //string max = GetLongest(names);
            //Console.WriteLine(max);
            //Console.ReadKey();

            //  80、用方法来实现:请计算出一个整型数组的平均值。保留两位小数
            //int[] numbers = { 1, 2, 7 };
            //double avg = GetAvg(numbers);
            //保留两位小数
            //string s = avg.ToString("0.00");
            //avg = Convert.ToDouble(s);
            //Console.WriteLine(avg);

            //Console.WriteLine(s);
            //Console.WriteLine("{0:0.00}", avg);
            //Console.WriteLine(avg);

            //double d = 3.148;
            //Console.WriteLine(d.ToString("0.00"));

            // Console.WriteLine("{0:0.00}",d);


            //1、写一个方法,用来判断用户输入的数字是不是质数  
            //再写一个方法 要求用户只能输入数字 输入有误就一直让用户输入
            while (true)
            {
                Console.WriteLine("请输入一个数字,我们将判断你输入的数字是否是质数");
                string strNumber = Console.ReadLine();

                int number = GetNumber(strNumber);

                bool b = IsPrime(number);
                Console.WriteLine(b);


                Console.ReadKey();
            }

        }

        public static bool IsPrime(int number)
        {
            if (number < 2)
            {
                return false;
            }
            else//>=2
            {
                //让这个数字从2开始除 除到自身的前一位
                for (int i = 2; i < number; i++)
                {
                    if (number % i == 0)
                    {
                        //给非质数准备的
                        return false;
                    }
                    //else
                    //{
                    //    return true;
                    //}
                }
                //给质数准备的
                return true;
            }
        }

        public static int GetNumber(string strNumber)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(strNumber);
                    return number;
                }
                catch
                {
                    Console.WriteLine("请重新输入");
                    strNumber = Console.ReadLine();
                }
            }
        }
        public static double GetAvg(int[] nums)
        {
            double sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                sum += nums[i];
            }
            return sum / nums.Length;
        }
        /// <summary>
        ///求一个字符串数组中最长的元素
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetLongest(string[] s)
        {
            string max = s[0];
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i].Length > max.Length)
                {
                    max = s[i];
                }
            }
            return max;
        }
    }
}

 练习三

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

namespace _02方法的练习
{
    class Programe
    {
        static void Main(string[] args)
        {
            //95、接受输入后判断其等级并显示出来。
            //判断依据如下:等级={优 (90~100分);良 (80~89分)
            //Console.WriteLine("请输入考试成绩");
            //int score = Convert.ToInt32(Console.ReadLine());

            //string level = GetLevel(score);
            //Console.WriteLine(level);
            //Console.ReadKey();


            //97、请将字符串数组{ "中国", "美国", "巴西", "澳大利亚", "加拿大" }中的内容反转
            string[] names = { "中国", "美国", "巴西", "澳大利亚", "加拿大" };
            Test(names);
            for (int i = 0; i < names.Length; i++)
            {
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();

            //98写一个方法 计算圆的面积和周长  面积是 pI*R*R  周长是 2*Pi*r

            //double r = 5;
            //double perimeter;
            //double area;
            //GetPerimeterArea(r, out perimeter, out area);
            //Console.WriteLine(perimeter);
            //Console.WriteLine(area);
            // Console.ReadKey();
        }


        public static void GetPerimeterArea(double r, out double perimeter, out double area)
        {
            perimeter = 2 * 3.14 * r;
            area = 3.14 * r * r;
        } 


        public static void Test(string[] names)
        {
            for (int i = 0; i < names.Length / 2; i++)
            {
                string temp = names[i];
                names[i] = names[names.Length - 1 - i];
                names[names.Length - 1 - i] = temp;
            }
        }

        public static string GetLevel(int score)
        {
            string level = "";
            switch (score / 10)
            {
                case 10:
                case 9: level = "优"; break;
                case 8: level = "良"; break;
                case 7: level = "中"; break;
                case 6: level = "差"; break;
                default:
                    level = "不及格";
                    break;
            }
            return level;
        }

    }
}
原文地址:https://www.cnblogs.com/hao-1234-1234/p/6104936.html