0

任务是创建一个评分系统,因此当用户输入他们的评分(满分 5)时,评分将显示在下面的星号中:

例如4 = ****

,但我相信我的代码编写正确,但它似乎仍然无法正确执行。

 Protected Sub btnRate_Click(sender As Object, e As EventArgs) Handles btnRate.Click

        Dim txtStar As Integer

        If txtStar = "1" Then
            lblStar.Text = "*"
        End If

        If txtStar = "2" Then
            lblStar.Text = "**"
        End If

        If txtStar = "3" Then
            lblStar.Text = "***"
        End If

        If txtStar = "4" Then
            lblStar.Text = "****"
        End If

        If txtStar = "5" Then
            lblStar.Text = "*****"
        End If

    End Sub
End Class

感谢您的帮助。

4

3 回答 3

2

试试这个。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim rate As Integer
    rate  = txtStar.Text
    lblStar.Text = String.Empty
    For index = 1 To rate
        lblStar.Text += "*"
    Next
End Sub

编辑

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim rate As Integer
    rate = txtStar.Text
    If rate = 1 Then
        lblStar.Text = "*"
    ElseIf rate = 2 Then
        lblStar.Text = "**"
    ElseIf rate = 3 Then
        lblStar.Text = "***"
    ElseIf rate = 4 Then
        lblStar.Text = "****"
    ElseIf rate = 5 Then
        lblStar.Text = "*****"
    Else
        lblStar.Text = String.Empty
    End If
End Sub
于 2013-01-02T23:32:57.050 回答
0

文本框也是如此txtStar。您应该使用Int32.ParseTryParse将文本转换为数字,然后您可以使用此字符串构造函数

Dim starCount As Int32
Dim canBeParsed = Int32.TryParse(txtStar.Text, starCount)
If canBeParsed Then
    lblStar.Text = New String("*"c, starCount)
End If
于 2013-01-02T23:15:34.070 回答
0

如果 txtStar 是一个文本框,那么您应该将此文本框的 Text 属性与您的常量进行比较

   If txtStar.Text = "1" Then
       lblStar.Text = "*"
   End If

等等 ...

但是不清楚代码中声明的整数变量的含义是什么

 Dim txtStar As Integer

您没有设置初始值,所以我认为这只是一个死代码,因此可以删除。
但是如果你想使用它,你需要分配一个值然后比较....

txtStar = Convert.ToInt32(txtStar.Text)
If txtStar = 1 Then
    lblStar.Text = "*"
End If

请注意我如何删除与字符串常量的比较并使用整数常量。
此外,这个变量的不同名称可能不会那么令人困惑......

于 2013-01-02T23:16:39.060 回答