如果文本与某物相等,则缩短代码

时间:2019-06-05 16:16:38

标签: vb.net

我是一名初学者程序员,我正在尝试使用Questions(想成为百万富翁)来开发游戏。但是,不幸的是,我有点卡住了,因为我想做的事情需要太频繁地重写代码。我可以简化一点吗?

第一个代码与Questions。

如果LbNumber.Text增加+1,则所有代码增加+1

   Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
        Dim Rand As New Random() 'Pentru extragerea unui ` Număr ` aleatoriu)
        Dim Number = Rand.Next(1, CountQListBox.Text) 'Exemplu, Un Număr intre (1,26)
        LBNumber.Text = Number 'In Label-ul nostru denumit, LBNumber, va apărea Numarul extras.
        If LBNumber.Text = ("1") Then 'Dacă Numărul extras va fi 1
            QuestionBox.Text = QListBoxLoad.Items(1) 'In TextBox-ul denumit, QuestionBox va apărea primul Item din ListBox 1.
            QListBoxLoad.SelectedItem = QListBoxLoad.Items(1) 'Vom selecta astfel primul Item, din ListBox 1.
        ElseIf LBNumber.Text = ("2") Then
            QuestionBox.Text = QListBoxLoad.Items(2)
            QListBoxLoad.SelectedItem = QListBoxLoad.Items(2)
        ElseIf LBNumber.Text = ("3") Then
            QuestionBox.Text = QListBoxLoad.Items(3)
            QListBoxLoad.SelectedItem = QListBoxLoad.Items(3)
        ElseIf LBNumber.Text = ("4") Then
            QuestionBox.Text = QListBoxLoad.Items(4)
            QListBoxLoad.SelectedItem = QListBoxLoad.Items(4)
        ElseIf LBNumber.Text = ("5") Then
            QuestionBox.Text = QListBoxLoad.Items(5)
            QListBoxLoad.SelectedItem = QListBoxLoad.Items(5)
        End If
    End Sub

第二个答案。

如果QListBox.Items增加+1,则AnswerLoadFile.Lines增加+1。但是其他的都没有改变价值。

 Private Sub QuestionBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionBox.TextChanged
        If QuestionBox.Text = QListBoxLoad.Items(1) Then
            Dim a As String : a = AnswerLoadFile.Lines(1)
            Debug.Print(indexThroughString(a, 2))
            Dim b As String : b = AnswerLoadFile.Lines(1)
            Debug.Print(indexThroughString(b, 3))
            Dim c As String : c = AnswerLoadFile.Lines(1)
            Debug.Print(indexThroughString(c, 4))
            Dim d As String : d = AnswerLoadFile.Lines(1)
            Debug.Print(indexThroughString(d, 5))
            AnswerA.Text = (indexThroughString(a, 2))
            AnswerB.Text = (indexThroughString(b, 3))
            AnswerC.Text = (indexThroughString(c, 4))
            AnswerD.Text = (indexThroughString(d, 5))
        ElseIf QuestionBox.Text = QListBoxLoad.Items(2) Then
            Dim a As String : a = AnswerLoadFile.Lines(2)
            Debug.Print(indexThroughString(a, 2))
            Dim b As String : b = AnswerLoadFile.Lines(2)
            Debug.Print(indexThroughString(b, 3))
            Dim c As String : c = AnswerLoadFile.Lines(2)
            Debug.Print(indexThroughString(c, 4))
            Dim d As String : d = AnswerLoadFile.Lines(2)
            Debug.Print(indexThroughString(d, 5))
            AnswerA.Text = (indexThroughString(a, 2))
            AnswerB.Text = (indexThroughString(b, 3))
            AnswerC.Text = (indexThroughString(c, 4))
            AnswerD.Text = (indexThroughString(d, 5))
        ElseIf QuestionBox.Text = QListBoxLoad.Items(3) Then
            Dim a As String : a = AnswerLoadFile.Lines(3)
            Debug.Print(indexThroughString(a, 2))
            Dim b As String : b = AnswerLoadFile.Lines(3)
            Debug.Print(indexThroughString(b, 3))
            Dim c As String : c = AnswerLoadFile.Lines(3)
            Debug.Print(indexThroughString(c, 4))
            Dim d As String : d = AnswerLoadFile.Lines(3)
            Debug.Print(indexThroughString(d, 5))
            AnswerA.Text = (indexThroughString(a, 2))
            AnswerB.Text = (indexThroughString(b, 3))
            AnswerC.Text = (indexThroughString(c, 4))
            AnswerD.Text = (indexThroughString(d, 5))
        ElseIf QuestionBox.Text = QListBoxLoad.Items(4) Then
            Dim a As String : a = AnswerLoadFile.Lines(4)
            Debug.Print(indexThroughString(a, 2))
            Dim b As String : b = AnswerLoadFile.Lines(4)
            Debug.Print(indexThroughString(b, 3))
            Dim c As String : c = AnswerLoadFile.Lines(4)
            Debug.Print(indexThroughString(c, 4))
            Dim d As String : d = AnswerLoadFile.Lines(4)
            Debug.Print(indexThroughString(d, 5))
            AnswerA.Text = (indexThroughString(a, 2))
            AnswerB.Text = (indexThroughString(b, 3))
            AnswerC.Text = (indexThroughString(c, 4))
            AnswerD.Text = (indexThroughString(d, 5))
        ElseIf QuestionBox.Text = QListBoxLoad.Items(5) Then
            Dim a As String : a = AnswerLoadFile.Lines(5)
            Debug.Print(indexThroughString(a, 2))
            Dim b As String : b = AnswerLoadFile.Lines(5)
            Debug.Print(indexThroughString(b, 3))
            Dim c As String : c = AnswerLoadFile.Lines(5)
            Debug.Print(indexThroughString(c, 4))
            Dim d As String : d = AnswerLoadFile.Lines(5)
            Debug.Print(indexThroughString(d, 5))
            AnswerA.Text = (indexThroughString(a, 2))
            AnswerB.Text = (indexThroughString(b, 3))
            AnswerC.Text = (indexThroughString(c, 4))
            AnswerD.Text = (indexThroughString(d, 5))
        End If

1 个答案:

答案 0 :(得分:0)

尝试:

...
$frooble = $form->get('frooble')->getData();
if (0 === $frooble) {
    $client->setFroobleEnabled(false);
    $client->setFroobleType(0);
} else {
    $client->setFroobleEnabled(true);
    $client->setFroobleType($frooble);
}

然后是这个

Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
    Dim Rand As New Random()
    Dim Number = Rand.Next(1, CountQListBox.Text)
    LBNumber.Text = Number
    QListBoxLoad.SelectedItem = QListBoxLoad.Items(Number)
    QuestionBox.Text = QListBoxLoad.Items(Number) 
End Sub