价格和面积倍增时的例外情况

时间:2014-01-31 21:57:26

标签: vb.net

我差不多已经完成了这个程序,但是我很难做出这个法案。

我的程序允许用户通过从1-3中做出选择从疼痛类型中进行选择。 1是奢侈品,£1.75另一个是标准£1.00,第三选择“经济”是0.40p。对于账单,我必须将油漆的价格乘以房间的面积。 我试过这样做:

    Dim paintprice As Double

    paintprice = paintType * areaofroom

    Console.WriteLine(paintType * areaofroom)

    Console.WriteLine(" You are a winner")

    Console.ReadLine()

执行此操作后,我收到错误消息:conversion from sting luxury to type double is

2 个答案:

答案 0 :(得分:0)

假设您的paintype可能包含“£”,您可以试试这个,就像Bjørn-RogerKringsjå所说:

Dim paintprice As Double

Dim priceweight As Double = If(paintype.Contains("£"), _
Double.Parse(paintype.Remove(paintype.IndexOf("£"), 1)), Double.Parse(paintype)) 
' This will remove the "£" and convert `paintype` to a double

paintprice = priceweight * areaofroom

答案 1 :(得分:0)

<强>原因

想想看,你正在尝试将字符串值"luxury"转换为双倍。

Dim d As Double = CType("luxury", Double)

将抛出:

Conversion from string "luxury" to type 'Double' is not valid.

<强>解决方案

你需要这样做:

Dim paintTypeDouble As Double = 0.0#

Select Case paintType
    Case "luxury"
        paintTypeDouble = 1.75#
    Case "standard"
        paintTypeDouble = 1.0#
    Case "economy"
        paintTypeDouble = 0.4#
    End Select

然后,假设areaofroom是一个整数类型:

paintprice = (paintTypeDouble * areaofroom)