【问题标题】:Huntington Motors亨廷顿汽车
【发布时间】:2018-06-05 10:43:34
【问题描述】:

所以我在 VB.NET 中进行了一些高级练习,但我对我正在做的事情有点僵硬。任务是“亨廷顿汽车公司的每个销售人员都分配了一个由四个字符组成的 ID 号。第一个字符是数字 1 或数字 2。1 表示销售人员销售新车,2 表示销售人员卖二手车,中间两个字符是销售人员的首字母,最后一个字符是字母F或字母P,字母F表示销售人员是全职员工,字母P表示他或她是兼职员工... 应用程序应允许销售经理根据需要输入销售人员的 ID 和销售汽车数量。应用程序应计算并显示以下四个类别中的每一个销售的汽车总数员工:全职员工、兼职员工、销售新车员工、销售二手车员工。” this is my interface 到目前为止,这是我的代码:

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles 
btnCalculate.Click
    'variables
    Dim strInput As String = ""
    Dim strOutput As String = ""
    Dim intFullTime As Integer = 0
    Dim intPartTime As Integer = 0
    Dim intNewCar As Integer = 0
    Dim intUseCar As Integer = 0
    Dim intNumSold As Integer
    strInput = txtId.Text
    ' changes the id number case to upper
    strInput = txtId.Text.ToUpper()
    ' sets the focus
      txtId.Focus()

   Integer.TryParse(txtNumberSold.Text, intNumSold)
    If strInput Like "[12][A-Z][A-Z][FP]" Then
        If strInput.Substring(0) = "1" Then
            intNewCar = intNewCar + intNumSold
            txtNewCar.Text = intNewCar.ToString
        Else
            intUseCar = intUseCar + intNumSold
            txtUsedCar.Text = intUseCar.ToString
        End If

        If strInput.Substring(3) = "F" Then
            intFullTime = intFullTime + intNumSold
            txtFullTime.Text = intFullTime.ToString
        Else
            intPartTime = intPartTime + intNumSold
            txtPartTime.Text = intPartTime.ToString
        End If
    End If

End Sub
End Class

现在我的问题是,当输入第一个字符为 1 的 ID 时,结果不会打印在 txt.NewCar.Text 上。另一个问题是,我如何计算售出的汽车数量?任何提示将非常感谢。我不确定我是否做得对。

【问题讨论】:

  • 您应该使用Chars,而不是使用Substring() 作为单个字符。

标签: .net vb.net visual-studio


【解决方案1】:

对于第一个问题,您错误地使用了Substring 函数,通过只传递一个值,您是在指示它从该点到末尾给您字符串。你应该做的是:

strInput.Substring(0, 1) ' Get the first character

 strInput.Substring(3, 1) ' Get the fourth character

对于第二个问题,intFullTimeintPartTime 都是局部变量,每次单击按钮时都会创建并初始化为 0。您应该将这两个变量移动为您的类的成员变量,然后每次单击按钮并解析/处理值时总计应累积。

【讨论】:

  • 我大部分都想通了。感谢您提供有用的提示!除了全职和兼职,一切都在积累。当我更改售出的汽车数量时,该数字保持不变或发生变化。但不加起来。有什么建议吗?
  • 您是否移动 intFullTimeintPartTime 变量成为表单/类的成员变量?如果您创建了新变量并将旧变量留在 Click 事件中,它将无法按您的意愿工作
  • 它正在工作!谢谢你的时间!由于某种原因,Visual Studio 出现故障。但一切正常!再次非常感谢!
猜你喜欢
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2020-01-26
  • 2018-09-14
相关资源
最近更新 更多