【发布时间】:2014-10-13 10:01:39
【问题描述】:
我目前正在执行一项任务,只是想在某事上得到一点帮助。对于我的代码,我必须从一组值中找到最低和最高值,然后将那些不是最高或最低的值加在一起(例如,1、2、3、4、5 --- 我会加 2+ 3+4)
所以我认为最好的方法是遍历数组并记录存储最高/最低值的位置。这就是我的问题所在,数组存储在Main方法中,我还没有找到在其他方法中访问它的方法。到目前为止我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Scoring {
class Program {
static void Main(string[] args) {
int[] scores = { 4, 7, 9, 3, 8, 6 };
find_Low();
ExitProgram();
}
static int find_Low() {
int low = int.MaxValue;
int low_index = -1;
foreach (int i in scores) {
if (scores[i] < low) {
low = scores[i];
low_index = i;
}
}
Console.WriteLine(low);
Console.WriteLine(low_index);
return low;
}
static void ExitProgram() {
Console.Write("\n\nPress any key to exit program: ");
Console.ReadKey();
}//end ExitProgram
}
}
我得到的错误是“名称'scores'在当前上下文中不存在。任何提示/帮助将不胜感激。
【问题讨论】:
-
这个程序中至少混合了三种编码风格。确定一种风格(提示:MS 风格非常适合 C#,因为所有库都使用它)并一致地使用它。