Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VB.net中计算单独班级的平均分?_Vb.net - Fatal编程技术网

如何在VB.net中计算单独班级的平均分?

如何在VB.net中计算单独班级的平均分?,vb.net,Vb.net,每当我试图从validateCores()类获取答案时,它总是将0显示为答案。我不能在同一个班上计算,因为这是我作业的一部分 Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Student As New myStudent Student.ValidateScores(TextBox1.Text, TextBox2.T

每当我试图从validateCores()类获取答案时,它总是将0显示为答案。我不能在同一个班上计算,因为这是我作业的一部分

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Student As New myStudent

    Student.ValidateScores(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    Label8.Text = Student.getAverage()


End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox1.ResetText()
    TextBox2.ResetText()
    TextBox3.ResetText()
    TextBox4.ResetText()
    Label8.ResetText()
End Sub
End Class
Public Class myStudent
Public Name As String
Public Average As Double

Public Sub ValidateScores(ByVal No1 As Integer, ByVal No2 As Integer, ByVal No3 As Integer)
    If (No1 >= 0 And No1 <= 100) And (No2 >= 0 And No2 <= 100) And (No3 >= 0 And No3 <= 100) Then
        Average = (No1 + No2 + No3) / 3
        Average = getAverage()
    Else
        MsgBox("Please enter the correct value from 0 to 100")
    End If
End Sub
Function getAverage() As Decimal
    Return getAverage
End Function
End Class
公共类表单1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
我的新学生
Student.validateStores(TextBox1.Text、TextBox2.Text、TextBox3.Text)
Label8.Text=Student.getAverage()
端接头
私有子按钮2\u单击(发送者作为对象,e作为事件参数)处理按钮2。单击
TextBox1.ResetText()
TextBox2.ResetText()
TextBox3.ResetText()
TextBox4.ResetText()
Label8.ResetText()
端接头
末级
公共课我的学生
公共名称作为字符串
公共海损为双倍
公共子ValidateCores(ByVal No1为整数,ByVal No2为整数,ByVal No3为整数)

如果(No1>=0和No1=0和No2=0和No3,如果您为将来的项目在项目属性和选项(VB默认值)中设置Option Strict,您将省去很多麻烦

不要在
myStudent
类中进行验证。您希望在用户界面代码中显示消息框,而不是在
myStudent
类中显示消息框。同一类可用于不识别消息框的用户界面。用户输入的验证与学生无关。只有属性和方法直接与学生相关的y应该在我的学生班上

我创建了你的字段属性。我添加了一个子新属性,这样你就不能在创建学生时设置类的所有数字属性

在确定输入是否在范围内之前,您需要确定它是否真的是一个数字。
.TryParse
方法将完成此操作

我知道你们知道如何计算平均数。把它们加起来,除以数字的计数

我们实际上只在验证之后创建
myStudent
类的一个实例

Public Class myStudent
    Public Property Name As String
    Public Property Average As Decimal
    Public Property Test1 As Decimal
    Public Property Test2 As Decimal
    Public Property Test3 As Double

    Public Sub New(t1 As Decimal, t2 As Decimal, t3 As Decimal)
        Test1 = t1
        Test2 = t2
        Test3 = t3
    End Sub

    Function getAverage() As Decimal
        Dim Avg = (Test1 + Test2 + Test3) / 3
        Return CDec(Avg)
    End Function
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Student As myStudent = Nothing
    Dim t1, t2, t3 As Decimal
    If Decimal.TryParse(TextBox1.Text, t1) OrElse Decimal.TryParse(TextBox2.Text, t2) OrElse Decimal.TryParse(TextBox3.Text, t3) Then
        If t1 >= 0 AndAlso t1 <= 100 AndAlso t2 >= 0 AndAlso t2 <= 100 AndAlso t3 >= 0 AndAlso t3 <= 100 Then
            Student = New myStudent(t1, t2, t3)
        Else
            MessageBox.Show("Please enter the correct value from 0 to 100")
        End If
    Else
        MessageBox.Show("Please enter the correct value from 0 to 100")
    End If
    Label8.Text = Student.getAverage().ToString("##0.00")
End Sub
公共课堂myStudent
作为字符串的公共属性名称
以十进制表示的公共财产平均数
公共属性Test1为十进制
公共属性Test2为十进制
公共财产测试3为双
公共子新(t1为十进制,t2为十进制,t3为十进制)
Test1=t1
Test2=t2
Test3=t3
端接头
函数getAverage()为十进制
尺寸平均值=(测试1+测试2+测试3)/3
返回CDec(平均值)
端函数
末级
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim Student As myStudent=无
尺寸t1、t2、t3为十进制
如果Decimal.TryParse(TextBox1.Text,t1)或lse Decimal.TryParse(TextBox2.Text,t2)或lse Decimal.TryParse(TextBox3.Text,t3),则

如果t1>=0,也就是t1=0,也就是t2=0,也就是t3,那么方法
getAverage()
的作用是什么?它只返回整数的初始值,即0。@AlexB。那么我该怎么办?我不能使用validatescores()方法作为输出。它总是以错误的形式出现。我认为如果是Decimal.TryParse(TextBox1.Text,t1),则该行是OrElse..应该是,也不是OrElse.谢谢,我还有几个问题。为什么我们不能在我的学生课堂上进行计算?为什么你把.ToString(##0.00”)放在学生的旁边。getaverage?@ameerul01如果我的答案解决了你的问题。请点击复选标记(勾选标记)接受它在答案的左边。@ameerul01平均值的计算在myStudent类中。传递给.ToString的参数是一个数字格式字符串。它告诉代码如何显示数字。在本例中,小数点后有两个数字。您可以更改字符串以任意方式显示。计算始终处于关闭状态。我尝试了组合n为30,60和90,但结果为3.33。