如何计算每行的总分和平均值(代表学生的分数)。并且还计算每个主题的总数和平均值。 [英] How do I compute the total score and average for each line (representing scores of a student). And also compute the total and average for each subject.

查看:99
本文介绍了如何计算每行的总分和平均值(代表学生的分数)。并且还计算每个主题的总数和平均值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.IO;



名称空间NonyeluCOMP2240A9

{

class program

{

static void Main(string [] args)

{

字符串行,主题;

int s;

int t,平均值;

StreamReader f = new StreamReader(sourceScores .txt);

StreamWriter报告=新的StreamWriter(scoresReport.txt);



while((line = f.ReadLine) ())!= null)

{

String [] strings = line.Split();

if(strings.Length == 3)

{

subject = strings [0];

s = int.Parse(strings [1]);

t = int.Parse(strings [2]);

average = s + t;

Console.WriteLine({0}的平均值是{1:c},subject,average / 5);

report.WriteLine({0}的平均值为{1:c},主题,平均值/ 5);

}

}

f.Close();

report.Close();

}

}

}



我尝试了什么:



到目前为止,我已经尝试过计算,平均值= s + t / 5;

Console.WriteLine({0}总费用为{1:c},主题,平均值/ 5);



我也尝试了average = s + t;,没有除以5,也没有工作。当我运行程序时,我得到了



未处理的异常:System.FormatException:输入字符串的格式不正确。

at System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer& number,NumberFormatInfo info,Boolean parseDecimal)

at System.Number.ParseInt32(String s,NumberStyles style,NumberFormatInfo info)
System.Int32.Parse(String s)


at NonyeluCOMP2240A9.Program.Main(String [] args)c:\ usersrs \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 2015\Projects\NonyeluCOMP2240A9 \ NonyeluCOMP2240A9 \Program.cs:26行

按任意键继续...



问题在下面,所以你可以理解它。



2.创建一个文本文件sourceScores.txt,其中包含以下几行:



数学阅读写作

90 80 100

62 78 90

78 98 65

45 5 6 78

88 65 85



请确保将此文件保存在项目文件夹的\bin\debug子文件夹中。



3.在Main方法中,编写代码以读取上述文件中的数据,并计算每行的总分和平均值(代表学生的分数)。并且还计算每个主题的总数和平均值。将原始分数以及总分和平均值写入文本文件scoresReport.txt。您的程序生成的文本文件可能如下所示:



数学阅读写作总平均值

90 80 100 237 90

62 78 90 230 77

78 98 65 241 80

45 56 78 179 60

88 65 85 237 79

362 377 418

72 75 84

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace NonyeluCOMP2240A9
{
class Program
{
static void Main(string[] args)
{
string line, subject;
int s;
int t, average;
StreamReader f = new StreamReader("sourceScores.txt");
StreamWriter report = new StreamWriter("scoresReport.txt");

while((line = f.ReadLine()) != null)
{
String[] strings = line.Split();
if(strings.Length == 3)
{
subject = strings[0];
s = int.Parse(strings[1]);
t = int.Parse(strings[2]);
average = s + t;
Console.WriteLine("The average of {0} is {1:c}", subject, average/5);
report.WriteLine("The average of {0} is {1:c}", subject, average/5);
}
}
f.Close();
report.Close();
}
}
}

What I have tried:

So far, I've tried computing, average = s + t / 5;
Console.WriteLine("Total cost of {0} is {1:c}", subject, average/5);

I also tried average = s + t;, without dividing by 5 and that didn'twork either. When I run the program, I get

"Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at NonyeluCOMP2240A9.Program.Main(String[] args) in c:\users\ify\documents\visual studio 2015\Projects\NonyeluCOMP2240A9\NonyeluCOMP2240A9\Program.cs:line 26
Press any key to continue . . .

The problem is below so you can understand it.

2. Create a text file, sourceScores.txt, which contains the following lines:

Math reading writing
90 80 100
62 78 90
78 98 65
45 56 78
88 65 85

make sure you save this file in the \bin\debug subfolder of your project folder.

3. In the Main method, write code to read in data from the above file and compute the total score and average for each line (representing scores of a student). And also compute the total and average for each subject. Write the original scores and the totals and averages to a text file, scoresReport.txt. Your text file generate from your program may look like this:

Math reading writing total averages
90 80 100 237 90
62 78 90 230 77
78 98 65 241 80
45 56 78 179 60
88 65 85 237 79
362 377 418
72 75 84

推荐答案

好的,对于输入文件的第一行,数学阅读写作,你的字符串数组将包含Math阅读写作



现在,当您尝试将字符串[1]解析为整数时,实际上是在尝试将读取解析为整数。这就是你收到错误的原因。


在while循环之外单独处理输入文件和输出文件的第一行。
Okay, for the first line of your input file, "Math reading writing", your strings array will contain "Math", "reading" and "writing".

Now, when you are trying to parse strings[1] to an integer, you are actually trying to parse "reading" to an integer. That is why you are getting error.

Deal with the first line of the input file and output file separately outside the while loop.


这篇关于如何计算每行的总分和平均值(代表学生的分数)。并且还计算每个主题的总数和平均值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆