【发布时间】:2014-08-24 09:25:05
【问题描述】:
(对不起,如果我问的问题太多了)
现在我只是想弄清楚如何重组我编写的程序以满足标准。我想将它分成不同的方法以使其更易于阅读,但我无法让不同的方法相互配合(例如变量范围错误)。
现在我的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Scoring {
class Program {
static int highOccurrence = 0;
static int lowOccurrence = 0;
static void Main(string[] args) {
int[] scores = { 4, 7, 9, 3, 8, 6 };
findScore(scores);
ExitProgram();
}
static int findOccurrence(int[] scores) { //find the number of times a high/low occurs
for (int i = 0; i < scores.Length; i++) {
if (low == scores[i]) {
lowOccurrence++;
//record number of time slow occurs
}
if (high == scores[i]) {
highOccurrence++;
//record number of times high occurs
}
}
}
static int findScore(int[] scores) {
int[] arrofNormal = new int[scores.Length];
int low = scores[0];
int high = scores[0];
int total = 0;
//int highOccurrence = 0;
//int lowOccurrence = 0;
for (int i = 0; i < scores.Length; i++) {
// if (low == scores[i]) {
// lowOccurrence++;
// //record number of time slow occurs
// }
// if (high == scores[i]) {
// highOccurrence++;
// //record number of times high occurs
// }
if (low > scores[i]) {
low = scores[i];
} //record lowest value
if (high < scores[i]) {
high = scores[i];
//record highest value
}
}
for (int x = 0; x < scores.Length; x++) {
if (scores[x] != low && scores[x] != high) {
arrofNormal[x] = scores[x];
//provides the total of the scores (not including the high and the low)
}
total += arrofNormal[x];
}
if (highOccurrence > 1) { //if there is more than 1 high (or 1 low) it is added once into the total
total += high;
if (lowOccurrence > 1) {
total += low;
}
}
Console.WriteLine("Sum = " + total);
return total; //remove not all code paths return.. error
}
static void ExitProgram() {
Console.Write("\n\nPress any key to exit program: ");
Console.ReadKey();
}//end ExitProgram
}
}
如您所见,它仍然是一项非常有进展的工作。我收到诸如“变量名”在当前上下文中不存在之类的错误,我知道这是一个范围错误,我该如何解决?建议将不胜感激:)
【问题讨论】:
-
怎么样:您负责正确格式化您的问题,没有那么多空格,所以任何试图帮助您的人都不需要浪费时间滚动?关于如何提出一个好问题,以及网站上的适当礼仪有整页,他们没有说明问题的数量,但他们确实提到了质量。
标签: c# variables methods scope