Visual Studio 如何在visual basic中循环?

czfnxgou  于 2023-01-09  发布在  其他
关注(0)|答案(2)|浏览(250)

所以我有一个赋值语句,我很难添加一个循环。我想为这个问题做3个有效的答案,任何其他输入都会导致代码说无效的车辆类型,并再次提问。
我试着做了几天,但我不能使循环,因为它只是结束时,我运行的代码或转到下一个问题,即使答案无效
该程序2019 Visual Basic代码:

Dim vehicle_type As string
Dim days_required as integer
Dim insurance_cover as string
Dim new_existing as string
Dim B_S_G As string
Dim total_hire_cost as single
Const lowemission as single =15.5
Const zero emission as single =20.0
Const electric as single 32.0

Vehicle_type = inputbox("enter any 3 vehicle types, lowemission, zeroemission or electric")

Do
  Msgbox("invalid vehicle type")
Loop until vehicle type <> lowemission or zeroemission or electric
mhd8tkvw

mhd8tkvw1#

给予这个:

Do
    vehicle_type = InputBox("enter any 3 vehicle types, lowemission, zeroemission or electric")
    If vehicle_type = "lowemission" Or vehicle_type = "zeroemission" Or vehicle_type = "electric" Then
        Exit Do
    End If
    MsgBox("invalid vehicle type")
Loop
gdx19jrr

gdx19jrr2#

我会创建一个数组来保存您的有效响应:

Dim index As Integer
Dim types() As String = {"lowemission", "zeroemission", "electric"}
Dim strTypes As String = String.Join(", ", types)
Do
    vehicle_type = InputBox("Valid Vehicle types:" & vbCrLf & strTypes, "Enter vehicle type").ToLower().Trim()
    index = Array.IndexOf(types, vehicle_type)
    If index = -1 Then
        MessageBox.Show("invalid vehicle type")
    End If
Loop While index = -1

看起来像:

相关问题