如何在Visual Basic中执行此操作?

时间:2014-02-18 13:40:38

标签: vb.net winforms

如何在Visual Basic中执行“a ++”和“b ++”?

Vb中的另一个代码是什么?

那里的名字只是一个例子。

        int a = 0;
        int b = 0;
        {
            if (ans1.Text == "James") 
            {
                a++;
            }
            else
            {
                b++;
            }
            if (ans2.Text == "Ryan")
            {
                a++;
            }
            else
            {
                b++;
            }
            if (ans3.Text == "Mac")
            {
                a++;
            }
            else
            {
                b++;
            }
            t1.Text = a.ToString();
            t2.Text = b.ToString(); 
        }

4 个答案:

答案 0 :(得分:3)

像这样:

a += 1
b += 1
(...)

答案 1 :(得分:1)

喜欢这个

DIM a as integer = 0
DIM b as integer = 0

If ans1.Text = "James" Then
    a += 1
Else
    b += 1
End If
If ans2.Text = "Ryan" Then
    a += 1
Else
    b += 1
End If
If ans3.Text = "Mac" Then
    a += 1
Else
    b += 1
End If
t1.Text = a.ToString()
t2.Text = b.ToString()

答案 2 :(得分:1)

您的问题已经得到解答,但我认为了解如何简化代码会很有用:

Dim correctAnswers As Integer = 0
Dim totalQuestions As Integer = 3'you need to modify this is you add more questions

'increment the number of correct answers for each one we find
correctAnswers += If(ans1.text = "James", 1, 0)
correctAnswers += If(ans2.text = "Ryan", 1, 0)
correctAnswers += If(ans3.text = "Mac", 1, 0)

'show the number of correct and incorrect answers
t1.Text = correctAnswers.ToString()
t2.Text = (totalQuestions - correctAnswers).ToString() 'show the number of incorrect questions

答案 3 :(得分:0)

后缀和前缀++都没有在Visual Basic中定义。

唯一可行的选择是使用a = a + 1(或者,在后面的BASIC中,a += 1)(注意语句终止符缺少;)。但请注意,这不会评估a之前的值,并且整个构造在C / C ++意义上不是表达式。你可以构建一个模仿a++的函数,但这太混淆了。