vb.net – Do..While …循环

前端之家收集整理的这篇文章主要介绍了vb.net – Do..While …循环前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的学习书对下面的代码做了如下陈述:

**“计算机评估Do …循环语句中的循环条件,以确定是否应该处理循环指令.在这种情况下,inputales<> String.Empty条件将输入销售变量的最大容量与String.Empty值.如您所知,String.Empty值表示零长度或空的字符串,如果inputsales变量为空,则循环条件计算为True,计算机处理循环指令.*如果另一方面inputales变量不为空,循环条件的计算结果为false,计算机跳过循环指令.

根据代码,我认为它是相反的:…虽然inputales值不为空,但它应该计算为true并处理循环,如果它是空的,它应该计算为false并跳过循环?请看下面.非常感谢你的帮助!

Option Explicit On
Option Strict On

Imports System.Globalization


Public Class SalesForm

    Private Sub exitButton_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub calcButton_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles calcButton.Click
        Const prompt As String = "Enter a sales amount. Click cancel to end."
        Const title As String = "Sales Entry"
        Dim inputsales As String
        Dim sales As Decimal
        Dim salesCounter As Integer
        Dim salesaccumulator As Decimal
        Dim salesAverage As Decimal
        Dim isconverted As Boolean

        inputsales = InputBox(prompt,title,"0")

        **Do While inputsales <> String.Empty
            isconverted = Decimal.TryParse(inputsales,NumberStyles.Currency,NumberFormatInfo.CurrentInfo,sales)

            If isconverted = True Then
                salesCounter = salesCounter + 1
                salesaccumulator = salesaccumulator + sales
            Else
                MessageBox.Show("Please re-entere the sales amount.","sales Express",MessageBoxButtons.OK,MessageBoxIcon.Information)
            End If
            inputsales = InputBox(prompt,"0")
        Loop**

        If salesCounter > 0 Then
            salesAverage = salesaccumulator / Convert.ToDecimal(salesCounter)
            averageLabel.Text = salesAverage.ToString("C2")
            Label2.Text = salesCounter.ToString
        Else
            averageLabel.Text = "0"
        End If
    End Sub
End Class
你是绝对正确的,这本书是错的(希望,作者只是偶然地颠倒了真/假;否则,我会得到另一本书).我的建议更正(添加一些):

As you know,the String.Empty value represents a zero length,or empty,string. If the inputsales variable is not empty,the loop condition evaluates to True,and the computer processes the loop instructions (and then jumps back to the top of the loop and reevaluates the condition). If,on the other hand,the inputsales variable is empty,the loop condition evaluates to False,and the computer skips over the loop instructions (and continues with the first statement after the loop).

正如@xanatos所说:恭喜你在别人的文字中抓住你的第一个错误.所以问题就是1,我会说这对你的编程生涯很​​有希望.

猜你在找的VB相关文章