字符串“”“”的无效强制转换异常

时间:2017-10-23 19:23:54

标签: .net vb.net

嗨,我现在在vb中曾经有过几次同样非常烦人的问题而且我不明白为什么。在这个代码上

 Module Module1

    Sub Main()
        While True
            Console.Title = "discount orders"
            Console.Clear()
            Console.WriteLine("please enter the value of the order {0}£", vbNewLine)
            Dim order As Double = CDbl(Console.ReadLine())
            Select Case order
                Case 0 To 1000
                    order = order
                Case 1000 To 2500
                    order = order * 0.95
                Case 2500 To 5000
                    order = order * 0.9
                Case 5000 To 10000
                    order = order * 0.85
                Case > 10000
                    order = order * 0.8

            End Select
            Console.WriteLine("the discounted price is £{0}", order)
            Console.Read()
        End While
    End Sub

End Module

它第一次工作但是当第二次运行循环时它在第8行中断,因为它无法从字符串转换为double

3 个答案:

答案 0 :(得分:1)

为什么会这样?

Console.Read()
...
Console.Clear()

读取单个字符,然后清除控制台。因此,当用户按下 Enter 时,将向控制台输入回车符和换行符,并且只读取回车符(char 13),然后清除控制台。换行符(char 10)仍在那里,用于下一个输入。使用Console.Read()来代替Console.ReadLine(),而不是Sub Main() Console.Title = "discount orders" While True Console.Clear() ' Console.Write so the £ is before the input Console.Write("please enter the value of the order {0}£", Environment.NewLine) ' this is the user's input Dim input = Console.ReadLine() Dim order As Double ' try to parse a number, if not this is false If Double.TryParse(input, order) Then Select Case order Case 0 To 1000 order = order Case 1000 To 2500 order = order * 0.95 Case 2500 To 5000 order = order * 0.9 Case 5000 To 10000 order = order * 0.85 Case > 10000 order = order * 0.8 End Select Console.WriteLine("the discounted price is £{0}", order) Else Console.WriteLine("'{0}' not a valid input. try again", input) End If ' ReadLine to consume both the carriage return and line feed Console.ReadLine() End While End Sub

此外,添加一些验证,以便在输入非数字值时您的应用程序不会崩溃。

{{1}}

实施例

有效条目:

  

请输入订单的价值   
£2345   
折扣价为2227.75英镑

无效条目:

  

请输入订单的价值   
£322K   
'322k'不是有效输入。再试一次

答案 1 :(得分:0)

我更喜欢在继续下一步之前验证输入。

Sub Main()
    While True
        Console.Title = "discount orders"
        Console.Clear()
        Console.WriteLine("please enter the value of the order {0}£", vbNewLine)
        Try
            Dim input As UInteger
            If UInteger.TryParse(Console.ReadLine, input) Then
                Dim order As Double = Convert.ToDouble(input)
                Select Case order
                    Case 0 To 1000
                        order = order
                    Case 1000 To 2500
                        order = order * 0.95
                    Case 2500 To 5000
                        order = order * 0.9
                    Case 5000 To 10000
                        order = order * 0.85
                    Case Is > 10000
                        order = order * 0.8
                End Select
                Console.WriteLine("the discounted price is £{0}", order)
                Console.Read()
            Else
                Console.WriteLine("Please insert only numbers")
                Console.Read()
            End If
        Catch ex As Exception
            Console.WriteLine("Error!- {0}", ex.Message)
            Console.Read()
        End Try
    End While
End Sub

答案 2 :(得分:0)

你遇到的问题是Console.Read()从控制台获取下一个字符,如果你点击输入那么这是两个字符 - "\r\n" - 所以{ {1}}获得一个,然后后续的Read立即返回,因为它认为ReadLine是输入的结尾。然后你得到了错误。

相反,您应该将"\n"设为.Read()并要求用户按Enter键继续。