C#数组求和,求最大数,最小数,平均值

以上是代码

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[10];
            int count = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = i + 1;
                Console.WriteLine(arr[i]);
                count += arr[i];
            }

            int max = arr[0];
            for (int i = 0; i < arr.Length; i++)
            {
                if (max<arr.Length)
                {
                    max = arr[i];
                }
            }
            int min = arr[0];
            for (int i = 0; i > arr.Length; i++)
            {
                if (min > arr.Length) //片段是否大于满足就最小的   最大数同理
                {
                    min = arr[i];
                }
            }
            Console.WriteLine("和="+count);
            Console.WriteLine("平均数="+count/arr.Length);
            Console.WriteLine("最大数"+max);
            Console.WriteLine("最小数" + min);
        }
    }
}

以上是java

package Arry;

public class avgarry {
public static void main(String[] args) {
    int [] temp=new int[10];
     int count=0;
    for (int i = 0; i < temp.length; i++) {
        temp[i]=i+1;
        System.out.println(temp[i]);
        count+=temp[i];
    }
    
    int max=temp[0];
    for (int i = 0; i < temp.length; i++) {
        if (max<temp.length) {
            max=temp[i];
        }
    }
    int samll=temp[0];
    for (int i = 0; i < temp.length; i++) {
        if (samll>temp.length) {
            samll=temp[i];
        }
    }
    System.out.println("总数"+count);
    System.out.println("平均数"+count/temp.length);
    System.out.println("最大数"+max);
    System.out.println("最小数"+samll);
}
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在C#中,可以使用循环来组的最大值和最小值。在给定的代码片段中,引用演示了如何找到组的最大值,而引用演示了如何找到组的最小值。 要找到组的最大值,可以使用以下步骤: 1. 首先,定义一个整变量max,并将其初始化为组的第一个元素a。 2. 然后,使用一个循环遍历组中的每个元素。在循环中,从索引0开始,逐个增加索引i,直到i小于组的长度a.Length。 3. 在循环体中,使用条件语句判断当前元素a[i]是否大于max。如果是,则更新max的值为当前元素a[i]。 4. 循环结束后,max的值将是组的最大值。 5. 最后,使用Console.WriteLine语句将max的值打印出来。 要找到组的最小值,可以使用类似的步骤: 1. 首先,定义一个整变量min,并将其初始化为组的第一个元素a。 2. 然后,使用一个循环遍历组中的每个元素。在循环中,从索引0开始,逐个增加索引i,直到i小于组的长度a.Length。 3. 在循环体中,使用条件语句判断当前元素a[i]是否小于min。如果是,则更新min的值为当前元素a[i]。 4. 循环结束后,min的值将是组的最小值。 5. 最后,使用Console.WriteLine语句将min的值打印出来。 所以,对于给定的组{10, 9, 6, 4234, 312},最大值是4234,最小值是6。 而对于组{10, 1, 5, 6123, 34},最大值是6123,最小值是1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值