计算平均值并将输出放在列表框中 [英] calculate average and put the output in listbox
问题描述
视觉基本问题,我需要计算平均值并将输出放在列表框中,但是我有错误?
1.编写一个计算学期平均成绩的程序.文本文件中的每行五行应包含学生的社会安全号码以及三个小时考试和期末考试的成绩. (期末考试计为两个小时的考试.)程序应显示每个学生的社会安全号码和学期平均数,然后是全班平均数.使用表中的数据:学生成绩: http://i54.tinypic.com/2gya6j6.jpg [ ^ ]
这就是我所做的:
Visual basic problem, I need to calculate average and put the output in listbox but i''m having an error?
1. Write a program to compute semester averages. Each set of five lines in a text file should contain a student''s social security number and the grades for three hourly exams and the final exam. (The final exam counts as two hourly exams.) the program should display each student''s Social Security number and semester average, and then the class average. Use the data in Table : Students grades: http://i54.tinypic.com/2gya6j6.jpg[^]
and this is what i did:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As IO.StreamReader = IO.File.OpenText("AVERAGE.TXT")
Dim SSN, Exam1, exam2, exam3, finalexam, average As Double
SSN = CDbl(sr.ReadLine)
Exam1 = CDbl(sr.ReadLine)
exam2 = CDbl(sr.ReadLine)
exam3 = CDbl(sr.ReadLine)
finalexam = CDbl(sr.ReadLine)
average = CDbl((Exam1 + exam2 + exam3 + finalexam + finalexam) / 5)
Dim fmtstr As String = "{0,-13} {1,6}"
With ListBox1.Items
.Clear()
.Add(String.Format(fmtstr, "Soc. Sec. No.", "Average"))
.Add(String.Format(fmtstr, SSN, average))
.Add(String.Format(fmtstr, SSN, average))
.Add(String.Format(fmtstr, SSN, average))
End With
End Sub
End Class
但是每次我点击按钮都结束程序:(
请我需要您的帮助
but every time i click on the button it''s ending the program :(
please i need your help
推荐答案
使用调试器.在第一行放置一个断点,然后逐行开始.使用手表或快速监视窗口检查变量等的值.
还使用尝试捕获 [
Use the debugger. Place a breakpoint to the first line and start going line by line. Use the watches or the quickwatch window to examine the values of the variables etc.
Also using a try-catch[^] structure would be reasonable in your code in order to handle possible IO problems.
文件"AVERAGE.TXT"在哪里?
检查它:如果它不在您的项目Bin/Debug目录中,那将是问题所在.
设置文件的完整路径(例如,可以是"C:\ Temp \ AVERAGE.TXT"),然后尝试尝试.
Where is the file "AVERAGE.TXT"?
Check it: if it isn''t in your projects Bin/Debug directory, then that will be the problem.
Set the full path to the file (which could be "C:\Temp\AVERAGE.TXT" for example) instead, and try that.
这篇关于计算平均值并将输出放在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!