我的最大最小值的数组代码不工作
本文关键字:代码 工作 数组 最小值 我的 | 更新日期: 2023-09-27 18:06:57
我有这段代码完美,但一旦我开始修复它一点,它开始给我错误,我不知道如何修复。帮助吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinMax
{
public class Exercise
{
public int Min(int[] numbers)
{
int m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
{
if (m > numbers[i])
{
m = numbers[i];
return m;
}
}
}
public int Max(int[] numbers)
{
int m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
{
if (m < numbers[i])
{
m = numbers[i];
return m;
}
}
}
public static int Main()
{
int [] nbrs = new int[10];
Random rnd = new Random();
int x = 0;
foreach (int i in nbrs)
{
int gen = rnd.Next(1, 501);
nbrs [x] = gen;
Console.WriteLine(nbrs [x] + ", ");
x++;
};
Exercise exo = new Exercise();
Console.WriteLine("The minimum of the array is {0}", exo.Min(nbrs));
Console.WriteLine("The maximum of the array is {0}", exo.Max(nbrs));
return 0;
}
}}
错误'prog.cs(11,20):错误CS0161: MinMax.Exercise.Min(int[])': not all code paths return a value' and 'prog.cs(25,20): error CS0161:
MinMax.Exercise.Max(int[])':不是所有的代码路径返回值'出现,但我不明白如何,因为它工作得很好。
像这样更新部分代码:
public int Min(int[] numbers)
{
int m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
{
if (m > numbers[i])
{
m = numbers[i];
}
}
return m;
}
public int Max(int[] numbers)
{
int m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
{
if (m < numbers[i])
{
m = numbers[i];
}
}
return m;
}
编辑:这将帮助你摆脱你得到的错误。但是您仍然需要弄清楚返回正确值的逻辑。
你得到这个错误的原因是,如果在情况下没有数字在数组int[] numbers
,你传递给方法满足条件if (m > numbers[i])
,代码将永远不会到达返回语句。
我已经注释掉了错误的部分,并做了一些解释,希望对你有所帮助。
public class Exercise
{
public static int Min(int[] numbers)
{
int m = numbers[0];
// Start at index 1, not 0. m can never be greater than numbers[0].
for (int i = 1; i < numbers.Length; i++)
{
if (m > numbers[i])
{
m = numbers[i];
//return m; // This returns at the first number smaller than numbers[0].
}
}
return m; // return the final result here. `if` statement may never get executed.
//but the method must return `m` anyway as a result
}
public static int Max(int[] numbers)
{
int m = numbers[0];
// Start at index 1, not 0. m can never be less than numbers[0].
for (int i = 1; i < numbers.Length; i++)
{
if (m < numbers[i])
{
m = numbers[i];
//return m; // This returns at the first number larger than numbers[0].
}
}
return m; // return the final result here. `if` statement may never get executed.
//but the method must return `m` anyway as a result
}
public static void Main() // Main method should not return value
{
int[] nbrs = new int[10];
Random rnd = new Random();
// dont use `foreach` on array when you want to set values to that array.
// also `for` has a counter itself. remove the x and put it in `for` statement.
for (int x = 0; x < nbrs.Length; x++)
{
int gen = rnd.Next(1, 501);
nbrs[x] = gen;
Console.WriteLine(nbrs[x] + ", ");
}
//Exercise exo = new Exercise();
// you dont have to create a new instance of the current class.
// just use the methods directly but make them static in order to access it.
Console.WriteLine("The minimum of the array is {0}", Min(nbrs));
Console.WriteLine("The maximum of the array is {0}", Max(nbrs));
}
}