重组计划要符合某些标准,可以使用一些建议

时间:2014-08-24 09:25:05

标签: c# variables methods scope

(对不起,如果我问了太多问题)

现在我只想弄清楚如何重组我为满足标准而编写的程序。我想将它拆分成不同的方法以使其更容易阅读,但我无法让不同的方法相互玩(例如变量范围错误)。

现在我的代码如下:

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

}
}

正如你所看到的,它仍然是一项关于进步的工作。我收到错误,如&#34;变量名称&#34;在当前上下文中不存在&#34;,我知道这是一个范围错误,我该如何解决?建议将非常感谢:)

2 个答案:

答案 0 :(得分:1)

这应该让你开始:)

您需要将highlow移到findScore方法之外,并使用0而不是scores[0]进行初始化。然后,在致电findScore之前,您必须致电findOccurrence,以允许两个变量在您需要时包含您想要的值。

此外,您看起来像是来自Java背景。在C#中,根据命名约定,所有方法都应以大写字母开头。

您可以从Linq中受益匪浅。例如,您可以找到low scores.Min()high scores.Max()

findOccurrence可以写得更简短:

static int FindOccurence(int[] scores)
{
    lowOccurrence = scores.Count(s => s == low);
    highOccurrence = scores.Count(s => s == high);
}

这样的事情会提高我的可读性。

这是Introduction to LINQ

答案 1 :(得分:1)

是的,您可以在10秒内在Linq中写这个,但我认为您正在尝试学习C#的基本方面,Linq不会对此有所帮助。 您的问题是,您正在尝试使用不同方法的变量(尽管它是静态的,但并不重要)。如果在特定范围内需要参数,可以向findOccurrence方法添加参数。

private static void findOccurrence(int[] scores, int low, int high)
    { //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
            }
        }
    }

在findScore()中你可以像这样调用上面的方法:

        findOccurrence(scores, low, high);

        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;
            }
        }

我希望这会按预期工作。玩得开心学习C#