繁体   English   中英

标记平均成绩的学生名单

[英]Mark list of students finding average

在这里,当我分别对student1的四个主题的平均值求和时,就像当我继续对student2进行求值时,他的平均值与student1相加。 为什么不计算每个学生的单独平均值? 请帮忙。

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication3
 {
     class stud
     {
         static void Main (string[] args)
         {
             double[,] studentavg = new double[3, 4];
             double total = 0;
             int ch = 0;
             int i, j;

             while (ch == 0)
             {
                 for (i = 0; i < studentavg.GetLength(0); i++)
                 {
                     Console.WriteLine("Enter mark of student : {0}", i + 1);

                     for (j = 0; j < studentavg.GetLength(1); j++)
                     {
                         Console.WriteLine("Enter mark : {0}", j + 1);
                         studentavg[i, j] = Convert.ToDouble(Console.ReadLine());
                         total += studentavg[i, j];
                     }
                     Console.WriteLine("Average is: {0}", (total / studentavg.GetLength(1)));
                     Console.Write("Enter 1 for exit OR 0 for continue: ");
                     ch = Convert.ToInt16(Console.ReadLine());
                 }
             }
             Console.ReadLine();
         }
     }
 }

您永远不会将学生之间的total重置为0。 尝试添加

total = 0

ch = Convert.ToInt16(Console.ReadLine());

您将行放置为“ double total = 0;”。 在while循环之外。 您是否不需要每次都将其初始化为零?

您还应该避免使用“ Convert.ToInt16”,而应将其替换为“ if(!int.TryParse(Console.ReadLine(),out ch)){错误,重新输入数字} else一切正常”尝试避免使用预定义的整数大小。 从字符串到数字的转换始终会失败。

暂无
暂无

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

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