导航
  • 主页
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm
  • WPF
  • 问答

未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中 是怎么回事?

独孤大虾 乐视网 资深总监  2002-11-06 08:33:17
这是一个很简单的控制台程序,看不出来语法上有什么问题,但是一编译调试就会出现异常:未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中, 其他信息:Input string was not in a correct format.
请问该怎么解决?


// 纪录30个考生5年内每年15次的考试成绩,并打印各次考试的平均成绩各个学生每学年平均成绩

using System;

class score
{
public static void Main()
{
int[ , , ] myscore = new int [ 5, 15, 30];
int[ , ] scoreAveragePerYear = new int [ 5, 30];
int[ , ] scoreAveragePerTest = new int [ 5, 15];

Random rnd = new Random();
int abscore = 0;

for ( int year = 0; year <= 4; year++)
for ( int number = 0; number <= 14; number++)
for ( int student = 0; student <= 29; student++)
{
abscore = ( int) ( (rnd.Next() * 100) + 1);
myscore[ year, number, student] = abscore;
}

for ( int year = 0; year <= 4; year++)
for ( int number = 0; number <= 14; number=number+1)
{
scoreAveragePerTest[ year, number] = 0;
for ( int student = 0; student <= 29; student++)
scoreAveragePerTest[ year, number] += myscore[ year, number, student];
scoreAveragePerTest[ year, number] /= 30;
Console.WriteLine ( "The score average of the Year { 0}, Test { 1} is: { 2}", year + 1, number + 1, scoreAveragePerTest[ year, number]);
}
for ( int year = 0; year <= 4; year++)
for ( int student = 0; student <= 29; student++)
{
scoreAveragePerYear[ year, student] = 0;
for ( int number = 0; number <= 14; number++)
scoreAveragePerYear[ year, student] += myscore[ year, number, student];
scoreAveragePerYear[ year, student] /= 15;
Console.WriteLine ( "The score average of the Year { 0}, Student { 1} is: { 2}", year + 1, student + 1,scoreAveragePerYear[ year, student]);
}
}
}
...全文
559 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zag 2002-11-06
错误来源楼上兄弟说的第二个错误.
回复
TheAres 2002-11-06
程序有两个错误,
一个是逻辑上的错误,也许语法不了解.
abscore = ( int) ( (rnd.Next() * 100) + 1);
这个地方不对,估计你是想得到一个1-100的随机数.这样是得不到的.
abscore = rnd.Next(1,101) ;才是.

另一个是格式上的错误,很隐蔽啊.
Console.WriteLine ( "The score average of the Year { 0}, Test { 1} is: { 2}", year + 1, number + 1, scoreAveragePerTest[ year, number]);
要改成:
Console.WriteLine ( "The score average of the Year {0}, Test {1} is: {2}", year + 1, number + 1, scoreAveragePerTest[ year, number]);

你 的 空 格 用 的 太 过 了 , 就 想 我 这 句 话 一 样 .
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.