[英]Mark list of students finding average
Here when I sum the average of four subjects of student1 separately and like that when I proceed with student2 his average is being added with student1. 在这里,当我分别对student1的四个主题的平均值求和时,就像当我继续对student2进行求值时,他的平均值与student1相加。 Why is a separate average for each student not calculated? 为什么不计算每个学生的单独平均值? Please help. 请帮忙。
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();
}
}
}
You are never resetting total
back to 0 between students. 您永远不会将学生之间的total
重置为0。 Try adding 尝试添加
total = 0
after 后
ch = Convert.ToInt16(Console.ReadLine());
You placed the line "double total = 0;" 您将行放置为“ double total = 0;”。 outside the while loop. 在while循环之外。 Don't you need to initialize it each time to zero? 您是否不需要每次都将其初始化为零?
You should also avoid "Convert.ToInt16" and replace it by "if (!int.TryParse(Console.ReadLine(), out ch)) { error, re-enter number } else all ok" Try to avoid predefined integer sizes. 您还应该避免使用“ Convert.ToInt16”,而应将其替换为“ if(!int.TryParse(Console.ReadLine(),out ch)){错误,重新输入数字} else一切正常”尝试避免使用预定义的整数大小。 And a conversion from string to a number can always fail. 从字符串到数字的转换始终会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.