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 が textBox の場合、このテキスト ボックスの 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 に答える
0

テキストtxtStarボックスも同様です。Int32.Parseまたはを使用TryParseしてテキストを数値に変換する必要がある場合は、次の文字列コンストラクターを使用できます。

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 に答える