作为学校的一部分,我的任务之一是写10个的码约10不同的搜索图案等在阵列中找到的最大值
对于这一个,我需要使用到线性搜索在定义的数组中找到最高和最低值,然后显示找到该值的次数。
继承人的代码,我想出了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Maxvaluefinder
{
class Program
{
static void Main(string[] args)
{
var array = [1, 31, 10, 9, 420, -5, 77, 420, 300, 99]; //Sets up the array
var maxvalue = 0; //Establishes variables for maximum value and the counter of maximum value.
var maxvaluecount = 0;
for (i = 1; i < array.Length; i++)
{
if (array[i] > maxvalue)
{
maxvalue = array[i];
maxvaluecount = 1;
}
if (array[i] == maxvalue)
{
maxvaluecount = maxvaluecount + 1;
}
}
Console.WriteLine("The highest number in this array was" + maxvalue + "which appeared a total of" + maxvaluecount + "times."); // Prints the final outcome.
}
}
}
截至目前,我不是100%确定如何了 “(I = 1;我< intArray.Length;我++)” 部分作品,'我'位'不存在于当前的情况下'
请帮忙?
此外,有点不相关:我如何测试在Microsoft Visual Studio中运行代码?
谢谢:)
你的第二个问题:按F5。顺便说一下,数组索引从零开始,所以你需要'for(var i = 0; i
@ O.Jones:当然,我们应该先创建一个C#控制台项目,然后将这些代码放入上述项目的文件中。 –
可能不是你的老师想要的,但也有'array.Max()'.... – BradleyDotNET