如何摆脱无尽的循环?

时间:2014-02-07 08:35:34

标签: c#

我是c#世界的新手。我一直在研究一个程序来从分数输入中确定学生成绩,这是1和100的范围。但它几乎无限地从头到尾循环。这是程序请问我能做什么......?

namespace ExamScore
{
    class YourExamScore
    {
        public string ExamScore(int[] value, int startindex, int endindex)
        {
            bool YourExamScore;
            var terminate = -1;


            for (var index = 0; index <= 100; index++)
            {

                if (YourExamScore = (value[90] <= value[99]))
                {
                    Console.WriteLine("You Have An Excellent Score");
                }
                else if (YourExamScore = (value[70] <= value[89]))
                {
                    Console.WriteLine("You Have a Good Score");
                }
                else if (YourExamScore = (value[50] <= value[69]))
                {
                    Console.WriteLine("You Have a Satisfactory Score");
                }
                else if (YourExamScore = (value[0] <= value[49]))
                {
                    Console.WriteLine("You Have a Satisfactory Score");
                }
                else
                {
                    return terminate.ToString();
                }
            }

            return "";

        }


        static void Main(string[] args)
        {
            int YourExamScore;
            {
                Console.WriteLine("Input Your Exam Score");
                YourExamScore = Convert.ToInt32(Console.ReadLine());
            }
            { 
            var e = new YourExamScore();
            var value = new int[100] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
           31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
           64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100};

                    Console.WriteLine(e.ExamScore(value, 90, 100));
                    Console.ReadKey();
                    Console.WriteLine(e.ExamScore(value, 70, 89));
                    Console.ReadKey();
                    Console.WriteLine(e.ExamScore(value, 50, 69));
                    Console.ReadKey();
                    Console.WriteLine(e.ExamScore(value, 0, 49));
                    Console.ReadKey();
            }

        }
    }
}

并输出此

  

你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高   你的分数很高

4 个答案:

答案 0 :(得分:2)

您根本不需要循环或数组:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Input Your Exam Score");
        var examScore = Convert.ToInt32(Console.ReadLine());

        var grade = GetGrade(examScore);
        Console.WriteLine(grade);
    }

    private static string GetGrade(int examScore)
    {
        if (examScore >= 90)
            return "Excellent";

        if (examScore >= 70)
            return "Good";

        return "Satisfactory";
    }
}

答案 1 :(得分:0)

你的if语句没有比较==他们正在分配=,如果评估的话,它将始终为真

答案 2 :(得分:0)

对于你应该写的条件:

int score = value[index];
if (90 <= score && score <= 99))
{
     Console.WriteLine("You Have An Excellent Score");
}
// ... etc.

希望这是你无限循环的意思。

答案 3 :(得分:0)

您可以更有效地重写程序:

public class ScoreRange {
    public int Min { get; set; }
    public int Max { get; set; }
    public string Message { get; set; }
}

你的主要人物:

int YourExamScore;
Console.WriteLine("Input Your Exam Score");
YourExamScore = Convert.ToInt32(Console.ReadLine());

        List<ScoreRange> ranges = new List<ScoreRange>();
        ranges.Add(new ScoreRange() {
            Min = 0,
            Max = 49,
            Message = "You Have a Satisfactory Score"
        });
        ranges.Add(new ScoreRange() {
            Min = 50,
            Max = 69,
            Message = "You Have a Satisfactory Score"
        });
        ranges.Add(new ScoreRange() {
            Min = 70,
            Max = 89,
            Message = "You Have a Good Score"
        });
        ranges.Add(new ScoreRange() {
            Min = 90,
            Max = 100,
            Message = "You Have An Excellent Score"
        });



        string message = ranges.First(x => x.Min <= YourExamScore && YourExamScore <= x.Max).Message;
        Console.WriteLine(message);

请注意,您应该检查您的考试成绩范围是否在0到100之间