我正在学校写一个项目(在.NET Fiddle的VB控制台上)并遇到错误。
在这项任务中,我们必须: 输入10个测试结果和学生姓名。 在输入数据时验证所有数据并拒绝不良数据 我们需要打印平均值(算术平均值),最高标记和谁得到它(假设每次测试最多1次),最低标记和谁得到它(假设每次测试1分钟)
我的个人密码遵循:
Imports System
Public Module Module1
Public Sub Main()
Dim sStudentName As String
Dim iStudentMark As Double
Dim iHighMark As Integer
Dim iLowMark As Integer
Dim iAve As Integer
For List = 1 to 10
Console.WriteLine("Input A Students Name")
sStudentName = Console.ReadLine()
Console.WriteLine("This student's Mark is")
iStudentMark = Console.ReadLine()
Do Until 25<= iStudent Or iStudentMark <= 100
Console.WriteLine("Error. Invalid Percentage. Re-input.")
iStudentMark = Console.ReadLine()
Loop
iAve = iAve + iStudentMark
If iStudentMark > iHighMark Then
iHighMark = iStudentMark
Console.Write("The Highest Percentage has changed. The top score is now " + iStudentMark + " Percent from " + sStudentName)
End If
If iStudentMark < iLowMark Then
iHighMark = iStudentMark
Console.Write("The Highest Percentage has changed. The top score is now " + iStudentMark + " Percent from " + sStudentName)
End If
Next
iAve = iAve/10
Console.WriteLine(iAve)
End Sub
End Module
我得到的错误信息是:
运行时异常(第-1行):从字符串“The Highest。”转换 百分比已变为“输入'Double'无效。
堆栈追踪:
[System.FormatException:输入字符串的格式不正确。]
[System.InvalidCastException:从字符串转换“最高 百分比已经变为“输入'Double'无效。]
我从来都不是最好的编码,非常感谢那些试图解决它的人。
答案 0 :(得分:0)
Console.ReadLine()
方法将返回一个字符串,您不能将其直接分配给double。你需要把它们变成双重的。
Console.WriteLine("This student's Mark is")
If Not Integer.TryParse(Console.ReadLine(), iStudentMark) Then
Console.WriteLine("Invalid input..! failed to convert")
End If
'you can proceed with iStudentMark