gpt4 book ai didi

c# - 如何在给定数组中找到最高,第二高,最低第二低的数字

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:13 25 4
gpt4 key购买 nike

任何人都可以告诉我如何找到给定数组中最高,第二高,最低第二低的数字

var numbers = new[] {855,3,64,6,24,75,3,6,24,45};

任何指针和建议都会很有帮助。谢谢

最佳答案

你也可以试试这个——

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class secondHighestLowest : System.Web.UI.Page
{
int[] arr = new int[10] { 45, 3, 64, 6, 24, 75, 3, 6, 24, 45 };

protected void Page_Load(object sender, EventArgs e)
{
secondHighestLowestNumber();
secoundLowestNumber();
}

private void secondHighestLowestNumber()
{
int firstHighestNumber = arr[0];
int secondHighestNumber = arr[0];
for(int i = 0; i<arr.Length; i++)
{
if (arr[i]>firstHighestNumber)
{
firstHighestNumber = arr[i];
}
}

for (int x = 0; x < arr.Length; x++)
{
if (arr[x]>secondHighestNumber && firstHighestNumber!=arr[x])
{
secondHighestNumber = arr[x];
}
}

Response.Write("secondHighestNumber---- " + secondHighestNumber + "</br>");
}

private void secoundLowestNumber()
{
int firstLowestNumber = arr[0];
int secondLowestNumber = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < firstLowestNumber)
{
firstLowestNumber = arr[i];
}
}

for (int x = 0; x < arr.Length; x++)
{
if (arr[x] < secondLowestNumber && firstLowestNumber != arr[x])
{
secondLowestNumber = arr[x];
}
}

Response.Write("secondLowestNumber---- " + secondLowestNumber + "</br>");
}
}

希望这对您有所帮助:)

关于c# - 如何在给定数组中找到最高,第二高,最低第二低的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com