[英]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.