如何将一个数组添加到整数c#
我正在尝试编写一个c#代码,用于获取锯齿阵列的学生成绩(每个学生的成绩数量可以不同)并计算每个学生的平均成绩。这里是我的代码:如何将一个数组添加到整数c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace students_avg
{
class Program
{
static void Main(string[] args)
{
int n,m,i,j,count=0,avg;
Console.WriteLine("please enter the number of students");
n = Convert.ToInt32(Console.ReadLine());
int [][] student = new int [n+1][];
for (i = 1; i <= n; i++)
{
Console.WriteLine("how many grades does student number " + i + "have?");
m = Convert.ToInt32(Console.ReadLine());
student[i] = new int[m];
Console.WriteLine("please enter student number " + i + "'s grades");
for (j = 1; j <= m; j++)
{
student[i] =new int[] {Convert.ToInt32(Console.ReadLine())};
count +=Convert.ToInt32(student[i]);
}
avg = count/m ;
Console.WriteLine("the student number " + i + "'s average is " + avg);
}
Console.ReadKey();
}
}
}
但我有问题,因为它不给我适当的平均。那么,我怎样才能以正确的方式添加学生的成绩呢?
我想用锯齿阵s是您的任务需求,所以我会坚持OP。如果不是这样,你可以使用List<List<int>>
作为Mark的建议。
我试着在你的代码中做最小的改动。
事情是,你是缺少声明你的数组“锯齿”的一部分,并试图增加总计数,你有一点问题。
static void Main(string[] args)
{
int n, m, i, j, count;
Console.WriteLine("please enter the number of students");
n = Convert.ToInt32(Console.ReadLine());
int[][] student = new int[n][];
for (i = 0; i < n; i++)
{
count = 0;
Console.WriteLine("how many grades does student number " + (i+1) + " have?");
m = Convert.ToInt32(Console.ReadLine());
student[i] = new int[m];
Console.WriteLine("please enter student number " + (i+1) + "'s grades");
for (j = 0; j < m; j++)
{
student[i][j] = Convert.ToInt32(Console.ReadLine());
count += Convert.ToInt32(student[i][j]);
}
var avg = count/m;
Console.WriteLine("the student number " + i + "'s average is " + avg);
}
Console.ReadKey();
}
谢谢,真的很有帮助 –
@SaraMolavi不客气。猜猜我忘了在显示平均值时添加“+ 1”。你可能需要改变它。为了学习的目的,我还建议你也看看德米特里的答案,因为它包括Linq和字符串插值主题。 – uTeisT
您也可以在打印值后将平均值设置为零 –
改变这一行:
int n,m,i,j,count=0,avg;
到:
int n,m,i,j,count=0;
double avg;
这行:
avg = count/m ;
到
avg = (double)count/(double)m ;
count=0;
另外:你要创建一个新的等级排列,每级输入这里
student[i] =new int[] {Convert.ToInt32(Console.ReadLine())};
这行应该是
student[i][j-1] =Convert.ToInt32(Console.ReadLine());
二:
count +=Convert.ToInt32(student[i]);
应该
count += student[i][j-1];
修改,现在你的代码尝试
static void Main(string[] args)
{
int n, m, i, j, count = 0, avg;
Console.WriteLine("please enter the number of students");
n = Convert.ToInt32(Console.ReadLine());
int[][] student = new int[n + 1][];
for (i = 1; i <= n; i++)
{
Console.WriteLine("how many grades does student number " + i + " have?");
m = Convert.ToInt32(Console.ReadLine());
student[i] = new int[m+1];
Console.WriteLine("please enter student number " + i + "'s grades");
for (j = 1; j <= m; j++)
{
student[i][j] = Convert.ToInt32(Console.ReadLine());
count += Convert.ToInt32(student[i][j]);
}
avg = count/m;
Console.WriteLine("the student number " + i + "'s average is " + avg);
avg = 0;
}
Console.ReadKey();
}
有什么特别的原因,试图用手球阵列时,你可以用列表的列表,并得到这一切通过LINQ? – Mark
您在循环外部和循环的每次执行中设置'student [i] = new ...',但不在循环中使用'j'。 'Convert.ToInt32(student [i])'传递内部数组作为对象,我认为应该抛出一个异常,因为int []不是'IConvertible'。我认为你的意思是'学生[i] [j] = Convert.ToInt32(Console.ReadLine()); count + = student [i] [j];'当我们已经知道这个值是一个'int'时,不需要在第二行调用Convert。你可能应该使用'int.Parse()'或甚至更好的'int.TryParse()',这样你可以处理错误的输入。 – pinkfloydx33
不需要在方法顶部声明循环变量。它们的范围应该是循环条件。也给你的其他变量有意义的名字。 –