使用for循环创建平均考试成绩

时间:2018-09-28 04:55:27

标签: c# for-loop

我正在尝试使用for循环找出平均4个测试成绩的测试成绩。似乎我要超过4,而不是停下来,并且不确定我做错了什么,因为它没有显示我的平均值。请告知:

        Console.WriteLine("Please enter your first test score");

        //variables
        double testScore = double.Parse(Console.ReadLine());
        double average = 0;

        for (int count = 0; count <= 4; count++) // Start the count from 0-4
        {
            //get the total
            average = average + testScore;

            Console.WriteLine("Please enter your other test score");
            testScore = double.Parse(Console.ReadLine());

            //Calculate and display 
            Console.WriteLine("The average of your test score is :", average / count);
        }


    }

5 个答案:

答案 0 :(得分:2)

如果我对您的理解正确,那么您需要一个小于符号,而不是小于或等于

for (int count = 0; count < 4; count++)

为什么要从0开始,即循环迭代0,1,2,3,4

或者(因为)您正在使用除法计数,因此您实际上应该从1开始

for (int count = 1; count <= 4; count++)

最后,您应该始终检查用户输入中是否有肮脏的小手指

while(!double.TryParse(Console.ReadLine(),out testScore))
    Console.WriteLine("You had one job!);

public static bool TryParse (string s, out double result);

  

将数字的字符串表示形式转换为其双精度   等效的浮点数。返回值指示是否   转换成功还是失败。


完整示例

double sum = 0;

for (int count = 0; count <= 4; count++) // Start the count from 0-4
{

    Console.WriteLine("Please enter your other test score");
    while(!double.TryParse(Console.ReadLine(),out testScore))
        Console.WriteLine("You had one job!);

    sum += testScore;

    //Calculate and display 
    Console.WriteLine($"The average of your test score is : {(sum / (double)count):N2}");
}

答案 1 :(得分:1)

您的for循环似乎重复了5次(i = 0、1、2、3、4(从i = 0到i = 4))

double average = 0;
double sum = 0;
int numberOfTests = 4;

for (int count = 0; count < numberOfTests; count++) // Start the count from 0-4
{
    Console.WriteLine("Please enter test score " + count); //Console.WriteLine($"Please enter test score {count}");

    double testScore = 0;

    while(!double.TryParse(Console.ReadLine(), out testScore))
    {
        Console.WriteLine("Enter a valid number");
    }

    //get the total
    sum = sum + testScore; //or sum += testScore;
}

//Calculate and display (needs to be outside or else gets printed 4 times)
Console.WriteLine("The average of your test score is : " + sum / numberOfTests);

答案 2 :(得分:1)

使用此代码,如果有任何查询对我进行注释,它将解决您的问题。

  static void Main(string[] args)
            {
                 Console.WriteLine("Please enter your first test score");

            //variables
            double testScore = double.Parse(Console.ReadLine());
            double average = 0;

            for (int count = 1; count <= 5; count++) // Start the count from 0-4
            {
                //get the total
                average = average + testScore;

                Console.WriteLine("Please enter your other test score");
                testScore = double.Parse(Console.ReadLine());
                average = average / count;
                //Calculate and display 
                Console.WriteLine("The average of your test score is {0}", average);
            }


            Console.ReadKey();

            }

答案 3 :(得分:0)

Average is not being displayed, because it is never passed to the console.

更改以下行:

Console.WriteLine("The average of your test score is :", 1);

Console.WriteLine(string.Format("The average of your test score is : {0}", average/count));

或这个(假设Console.Writeline隐式处理字符串格式)。

Console.WriteLine("The average of your test score is : {0}", average / count);

答案 4 :(得分:0)

  

请尝试以下代码:

    //variables
    double testScore = 0
    double average = 0;
    double sum = 0;
    for (int count = 1; count <= 4; count++) // Start the count from 0-4
    {
        //get user input
        Console.WriteLine("Please enter test{count} score");
        testScore = double.Parse(Console.ReadLine());

        //now get the sum
        sum = sum + testScore ;
        //Calculate average score
        average = sum / count;
        //Now display the average
        Console.WriteLine("The average of your test score is :", average);
    }
  

以下是您在代码中所犯的错误:

  1. 运行从count = 0到count = 4的循环,这将导致错误的除法。对于第一次迭代,您有一个testScore但count = 0,它将使devide产生0错误。
  2. 您正在将testScore输入用于循环迭代,但没有在同一迭代中计算平均值。因此,它将永远不会使用最新的testScore输入来计算平均值。