简体   繁体   English

C# - 保存键入的值

[英]C# - Save typed values

I'm making a console program which you have to say how many students are in the class and how many tests you had on this class, so it'll give you at the end the student name with its final score.我正在制作一个控制台程序,您必须说明班上有多少学生以及您在这门课上进行了多少次测试,因此它会在最后为您提供学生姓名及其最终分数。 The problem is that I can't make the final calculus=(sum/numberOfTests).问题是我不能做最终的微积分=(sum/numberOfTests)。 It only take the last grade you told the program .它只需要你告诉程序的最后一个年级 Here is the code.这是代码。

namespace Repeat
{
    class Program
    {
        static void Main(string[] args)
        {
            //VARIABLES         
            float numberOfStudents;
            float numberOfTests;
            float average = 0;
            float grade;
            float sum = 0;
            string studentName;

            Console.WriteLine("Type the number of students");
            float.TryParse(Console.ReadLine(), out numberOfStudents);
            Console.WriteLine("Type the number of tests");
            float.TryParse(Console.ReadLine(), out numberOfTests);

            Console.WriteLine("");

            for (int i = 1; i <= numberOfStudents; i++)
            {
                Console.Write("Student name {0}: ", i);
                studentName = Console.ReadLine();

                for (int p = 1; p <= numberOfTests; p++)
                {
                    Console.Write("Test {0} grade: ", p);
                    float.TryParse(Console.ReadLine(), out grade);

                    average = sum / numberOfTests;
                }

                if (average < 7)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Average: " + average);
                    Console.ResetColor();
                }
                else if (average >= 7)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Average: " + average);
                    Console.ResetColor();
                }

                Console.WriteLine("");
            }

            ///END
            Console.ReadKey();
        }
    }
}

actually you didn't set and update the sum value, so it was always 0 , fix your inner for loop like this实际上你没有设置和更新sum值,所以它总是0 ,像这样修复你的内部 for 循环

   sum = 0;
   for (int p = 1; p <= numberOfTests; p++)
   {
        Console.Write("Test {0} grade: ", p);
        float.TryParse(Console.ReadLine(), out grade);
        sum = sum + grade;
        average = sum / numberOfTests;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM