如何为二维数组中的所有值设置整体if语句?

时间:2014-12-16 00:42:44

标签: c# arrays

目前我有一个[5,5] 2d数组加载如下:

double[,] evaluation = 

{ {1.0,0.8,0.9,1.0,0.6},

 {0.2,0.9,0.5,0.6,0.7},

 {0.5,1.0,1.0,1.5,0.9},

{0.9,0.9,0.9,1.0,1.0},

{1.0,0.9,1.0,0.8,0.9}};

这些值都是同行评估分数,包括自我评估。我手动删除了自我评估,因为它们不包括在内:

eval1 = (evaluation[1, 0] + evaluation[2, 0] + evaluation[3, 0] + evaluation[4, 0]) / 4;

eval2 = (evaluation[0, 1] + evaluation[2, 1] + evaluation[3, 1] + evaluation[4, 1]) / 4;
eval3 = (evaluation[0, 2] + evaluation[1, 2] + evaluation[3, 2] + evaluation[4, 2]) / 4;
eval4 = (evaluation[0, 3] + evaluation[1, 3] + evaluation[2, 3] + evaluation[4, 2]) / 4;
eval5 = (evaluation[0, 4] + evaluation[1, 4] + evaluation[2, 4] + evaluation[3, 4]) / 4;

eval1,2,3,4,5是我以后可以使用它们链接到特定的学生。 (例如:如果学生=学生1,最终成绩= projectGrade * eval1,如果为2,则使用eval2等。)

现在,我需要做的是将所有值设置为1到1以上

我该如何设置?我不知道怎么做而不说:

“如果评估[0,1]> 1,如果评估[0,2]> 1,如果评估[0.3]> 1 ......等”

我可以使用一个适合所有语句吗?

3 个答案:

答案 0 :(得分:1)

正如Edsger W. Dijkstra所说:

  

两个或更多,使用for

因此,我们走了......

for(int i = 0; i < evaluation.length; i++) {
    for(int j = 0; j < evaluation[i].length; j++) {
        if(evaluation[i][j] > 1.0d) {
            evaluation[i][j] = 1.0d;
        }
    }
}

答案 1 :(得分:1)

就个人而言,我会选择OOP(面向对象编程)的路线。 基本上,将一切都视为对象。一门课程有很多同行,一个同行 有很多分数。当这样做时,对于多少同伴或分数没有限制 你可以评估。或者,您也可以轻松计算班级平均值 或同伴的平均值。我在下面提供了一个基本示例:

public Course
{
    // Course code
    private string _code;
    public string Code
    {
        get { return this._code; }
        set { this._code = value; }
    }

    // Course Name
    private string _name;
    public string Name
    {
        get { return this._name; }
        set { this._name = value; }
    }

    // A course has many peers
    private List<Peer> _peers;
    public List<Peer> Peers
    {
        get { return this._peers; }
        set { this._peers = value; }
    }

    // A new course is always instantiated with a new empty list of peers.
    public Course()
    {
        this._peers = new List<Peer>();
    }

    // A course can be instantiated with a course name and code
    public Course(string name, string code)
        : this()
    {
        this._name = name;
        this._code = code;
    }

    // A course can have a peer added
    public void AddPeer(Peer peer)
    {
        this._peers.Add(peer);
    }

    // A course average can be calculated
    public double GetCourseAverage()
    {
        int peerCount = this._peers.Count();
        double totalScores = 0;

        foreach (var peer in this.peers)
            totalScores += peer.Evaluate();

            // Prevent DivideByZeroException from being thrown
            if (totalScores == 0)
                 return 0;

        return totalScores / peerCount;
    }
}

public Peer
{
    // Peer name
    private string _name;
    public string Name
    {
        get { return this._name; }
        set { this._name = value; }
    }

    // A peer has a list of scores
    private List<Score> _scores;

    // A new peer is always instantiated with a new empty list of scores.
    public Peer()
    {
        this._scores = new List<Score>();
    }

    // A new peer can be instantiated with his or her name
    public Peer(string name)
        : this();
    {
        this._name = name;
    }

    // A score can be added to a peer's list of scores
    public void AddScore(Score score)
    {
        if (score != null)
            this._scores.Add(score);
    }

    // A peer an be evaluated
    public double Evaluate()
    {
        // Do your if evaluation conditions in here
        // if score.Value > 1, etc.
        double result = 0;
        foreach (var score in this._scores)
            result += score.Value;

        return result;
    }
}

public Score
{
    private double _value;
    public double Value
    {
        get { return this._value; }
        set { this._value = value; }
    }

    public Score()
    {
        // Nothing to initialize 
    }

    // A score can be initialized with a value
    public Score(double value)
        : this()
    {
        this._value = value;
    }
}

static int Main(string[] args)
{   
    string _courseName = "Science";
    string _courseCode = "SCI123";
    string _peerName = "Sam";

    // Create a course
    Course _course = new Course(_courseName, _courseCode);

    // Add a peer called Sam
    _courses.AddPeer(new Peer(peerName);

    // Find sam and add a score
    if ((found = _courses.FirstOrDefault(peer => peer.Name = peerName) != null)
    {
        // Sam was found, lets add a few scores for sam
        found.Scores.Add(new Score(0.9));
        found.Scores.Add(new Score(0.8));
        found.Scores.Add(new Score(0.5));
        found.Scores.Add(new Score(0.7));
        found.Scores.Add(new Score(0.8));

        // Lets evaluate Sam
        var result = found.Evaluate();
        Console.WriteLine(string.Format("{0} total score was {1}.", found.Name, result));
    }
}

答案 2 :(得分:0)

就个人而言,我会使用&#34;用于&#34;循环。

由于您正在编写C#,您可能希望考虑一个Linq表达式: