-1

我正在制作一个程序,允许用户查看歌曲信息、播放其中的片段并购买选定的歌曲。

并允许用户点击购买按钮购买指定的曲子。

退房时:

如果用户没有购买任何曲子,他们将无法结帐,但他们可以退出程序。

使用 InputBox 以便用户可以输入他们的销售税率。由于用户正在输入一个值,因此您必须对他们的输入执行数据验证。

允许用户通过单击 InputBox Cancel 按钮取消签出过程。

当输入框显示时,文本框应该有焦点,当添加了错误的税值时,错误的值应该被清除,文本框应该重新获得焦点。

使用 Write/Writeline 创建一个名为 PurchaseOrder.txt 的采购订单文本文件,其中包括文件的创建日期和采购清单、小计、税收和总计。

当我单击所选歌曲的“购买”按钮并单击“结帐”按钮时,我得到一个输入框,输入数值,但是,它继续一遍又一遍地循环。我不明白为什么会这样。请参考下面代码中的 cmdCheckOut_Click 子例程。我认为这就是我得到错误的地方。

这是代码:

   Public Structure musicInfo
    <VBFixedString(30)> Public title As String
    <VBFixedString(20)> Public artist As String
    <VBFixedString(20)> Public genre As String
    <VBFixedString(10)> Public duration As String
    Public year As Integer
    Public price As Double
    <VBFixedString(15)> Public songFileName As String
   End Structure

Public Const NoOfTunes = 5
Public songs(NoOfTunes - 1) As musicInfo


Option Explicit On
Imports System.IO
Public Class frmTunes
  Public index As Integer
  Public purchaseCount As Integer
  Public purchasePrice(10) As Decimal
  Public purchaseTitle(10) As String
  Dim decimal1 As Decimal
  Dim decimal3 As Decimal
  Dim decimal4 As Decimal
Private Sub frmTunes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim i As Integer

    FileOpen(1, "music.dat", OpenMode.Random, , , Len(songs(0)))
    For i = 0 To NoOfTunes - 1
        FileGet(1, songs(i))
    Next
    FileClose(1)

    cmdPrevious.Visible = False

    DisplaySong(0)
End Sub

Sub DisplaySong(ByVal i As Int32)
    lblTitle.Text = songs(i).title
    lblArtist.Text = songs(i).artist
    lblGenre.Text = songs(i).genre
    lblDuration.Text = songs(i).duration
    lblYear.Text = Convert.ToString(songs(i).year)
    lblPrice.Text = Convert.ToString(songs(i).price)
End Sub

Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
    My.Computer.Audio.Stop()
End Sub

Private Sub cmdPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPurchase.Click
    purchaseTitle(purchaseCount) = lblTitle.Text
    purchasePrice(purchaseCount) = Convert.ToDecimal(lblPrice.Text)
    purchaseCount = (purchaseCount + 1)
End Sub

Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
    index = (index - 1)
    If (index < 4) Then
        cmdNext.Visible = True
    End If
    If (index = 0) Then
        cmdPrevious.Visible = False
    End If
    DisplaySong(index)
End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
    index = (index + 1)
    If (index = NoOfTunes - 1) Then
        cmdNext.Visible = False
    End If
    If (index > 0) Then
        cmdPrevious.Visible = True
    End If
    DisplaySong(index)
End Sub

Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlay.Click
    My.Computer.Audio.Play(songs(index).songFileName)
End Sub

Private Sub cmdCheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOut.Click
    Dim str1 As String = ""
    If (purchaseCount = 0) Then
        MsgBox("You have not ordered any items!", MsgBoxStyle.Exclamation, "Order Error")
    Else
        Do Until ((IsNumeric(str1) And (Decimal.Compare(decimal3, Decimal.Zero) <= 10)) And (Decimal.Compare(decimal3, (10D)) >= 0))
            str1 = InputBox("Enter your tax rate as a %" & vbCrLf & "between and including 0 - 10:", "Tax Rate", "", -1, -1)
            If (str1 <> "") Then
                If (Not IsNumeric(str1)) Then
                    MsgBox("You must enter a numeric tax rate", MsgBoxStyle.Exclamation, "Tax Rate Error")
                Else
                    Dim decimal3 As Decimal = Convert.ToDecimal(str1)
                    If ((Decimal.Compare(decimal3, Decimal.Zero) < 0) Or (Decimal.Compare(decimal3, (10D)) > 0)) Then
                        MsgBox("You must enter a tax rate between and including 0% - 10%", MsgBoxStyle.Exclamation, "Tax Rate Error")
                    End If
                End If
            End If
        Loop
        Dim StreamWriter As StreamWriter = File.CreateText("PurchaseOrder.txt")
        StreamWriter.WriteLine("For Purchases dated: " & DateTime.Now.ToLongDateString())
        StreamWriter.WriteLine()
        Dim num2 As Integer = (purchaseCount - 1)
        Dim num1 As Integer = 0
        Do While (num1 <= num2)
            StreamWriter.Write(Strings.FormatCurrency(CType(Me.purchasePrice(num1), Decimal) & "     "))
            StreamWriter.WriteLine(purchaseTitle(num1))
            Dim decimal1 As Decimal = Decimal.Add(Nothing, purchasePrice(num1))
            num1 = (num1 + 1)

        Loop
        StreamWriter.WriteLine("------")
        StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal1, Decimal)) & "  Subtotal")
        Dim decimal2 As Decimal = New Decimal(((Convert.ToDouble(decimal3) * 0.01) * Convert.ToDouble(decimal1)))
        StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal2, Decimal)) & "  Tax")
        StreamWriter.WriteLine("------")
        Dim decimal4 As Decimal = Decimal.Add(decimal1, decimal2)
        StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal4, Decimal)) & "  Total")
        MsgBox("Purchase Order has been created", MsgBoxStyle.OkOnly)
        StreamWriter.Close()
        Me.Close()
    End If
End Sub
End Class
4

1 回答 1

0

基本上它看起来像你使用 Decimal.Compare 错误。,看看这是否有帮助:

    Do 
        str1 = InputBox("Enter your tax rate as a %" & vbCrLf & "between and including 0 - 10:", "Tax Rate", "", -1, -1)
        If (str1 <> "") Then
            If (Not IsNumeric(str1)) Then
                MsgBox("You must enter a numeric tax rate", MsgBoxStyle.Exclamation, "Tax Rate Error")
            Else
                Dim decimal3 As Decimal = Convert.ToDecimal(str1)
                If decimal3 < 0 Orelse decimal3 > 10 Then
                    MsgBox("You must enter a tax rate between and including 0% - 10%", MsgBoxStyle.Exclamation, "Tax Rate Error")
                End If
            End If
        End If
    Loop Until (IsNumeric(str1) Andalso (decimal3 <= 10 Andlso decimal3 >= 0))
于 2013-11-11T16:06:56.233 回答