c# 方法小练习

1、求任意两个整数之间的所有整数的和:

先说下我的思路:

方法一:首选我做了一个判断,变量交换,保证了参数一比参数二小,把最小值赋值给一个总和变量,方便累加,每次循环都让总和变量+最小值+索引。

方法二:还是先做判断,之后把 最小值 直接赋值 i ,每次循环 i++。

// 方法一:
static void Main()
{
    GetSum(5, 0);
}
public static void GetSum(int a, int b)
{
    if(a > b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
    }
    int sum = a;
    int len = b - a;
    for (int i = 1; i < len+1; i++)
    {
        sum += a + i;
    }
    Console.WriteLine($"{sum}");
    Console.ReadLine();
}
// 方法二:
static void Main()
{
    int res = GetSum(1, 4);
    Console.WriteLine(res);
    Console.ReadLine();
}
public static int GetSum(int a, int b)
{
    if (a > b)
    {
        int tem = a;
        a = b;
        b = tem;
    }
    int sum = 0;
    for(int i = a; i <= b; i ++)
    {
        sum += i;
    }
    return sum;
}

2、用户只能输入数字:

下边主要用了递归来解决此问题:如果用输入的个数不正确,再次调用此方法。

static void Main()
{
    Console.WriteLine("请重新输入:");
    string num = Console.ReadLine();
    CheckValue(num);
}
public static void CheckValue(string a)
{
    try
    {
        int num = Convert.ToInt32(a);
        Console.WriteLine("正确!");
        Console.ReadKey();
    }
    catch
    {
        Console.WriteLine("格式不正确,请重新输入:");
        string num = Console.ReadLine();
        CheckValue(num);
    }
}

3、要求第一个数字必须比第二个数字小,否则重新输入:

也是用了递归来解决此问题,下边没有try{ }catch{ }, 因为有可能转换整数失败,明白就行,但实际开发中还需严谨!!

4、用方法实现,求字符串数组中字符串最长的元素

// 下边有个小地方需要注意:i直接从1开始即可,因为数组第一位与第一位之间不需要去比较!!!
public static void getLongString(string[] arr)
{
    string maxLength = arr[0];
    for (int i = 1; i < arr.Length; i++)
    {
        if (arr[i].Length > maxLength.Length)
        {
            maxLength = arr[i];
        }
    }
    Console.WriteLine(maxLength);
    Console.Read();
}

5、用方法实现,求整数型数组的平均值且返回2位小数:

获取到平均数之后,使用先ToString把把double类型的值格式化,然后在把格式化的值转为double类型

class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 7 };
        double r = getTwoDecimal(arr);
        string str = r.ToString("0.00");
        double dou = Convert.ToDouble(str);
        Console.WriteLine(dou);
        Console.Read();
    }
    public static double getTwoDecimal(int[] arr)
    {
        double res = 0;
        for (int i = 0; i < arr.Length; i ++)
        {
            res += arr[i];
        }
        return res / arr.Length;
    }
}

6、使用方法,实现数组元素逆序:

由于数组是引用类型,所以最后数组自身也改变了(当然也可以直接返回一个数组);

规律:循环圈数 = 数组长度 / 2 (如:三个元素,只需循环一圈即可;4个元素,循环2圈 ···)

class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 7, 5, 2, 7, };
        ReverseArrary(arr);

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

    }
}

7、求任意多个数间的最大值

class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 7, 5, 2, 7, };
        int r = GetMaxValue(1, 2, 7, 5, 2, 7);
        Console.WriteLine(r);
        Console.Read();

    }
    public static int GetMaxValue(params int[] arr)
    {
        int len = arr.Length;
        int maxValue = arr[0];
        for (int i = 0; i < len; i ++)
        {
            if (maxValue < arr[i])
            {
                maxValue = arr[i];
            }
        }
        return maxValue;
    }
}

8、使用冒泡排序对下边数字元素进行升序:

int arr = { 8, 3, 1, 4, 0, 1 }
class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 8, 3, 1, 4, 0, 1 };
        MaoPao(arr);
    }
    public static void MaoPao(int[] arr)
    {
        for (int i = 0; i < arr.Length; i ++)
        {
            for (int j = i + 1; j < arr.Length; j ++)
            {
                if (arr[i] > arr[j])
                {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42778001/article/details/108876620