如何在Visual Basic应用程序中显示用户的全名?

时间:2014-11-12 14:32:08

标签: vb.net

如何在应用程序运行后将用户的完整名称显示为输出? 这是Visual Basic的控制台应用程序。

' Airline.vb - This program determines if an airline passenger is 
' eligible for a 20% discount. 
Option Explicit On
Option Strict On
Module Airline 
   Sub Main()
      Dim passengerName As String = ""  ' Passenger's name
      Dim ageString As String = ""      ' String version of passenger's age
      Dim passengerAge As Integer = 0   ' Passenger's age

      ' This is the work done in the housekeeping() procedure 
      passengerName = InputBox$("Enter passenger's name: ")
      ageString = InputBox$("Enter passenger's age: ")

      ' This is the work done in the detailLoop() procedure 
      passengerAge = Convert.ToInt32(ageString)

        ' Test to see if this customer is eligible for a 20% discount 
        If passengerAge <= 7 Then
            System.Console.WriteLine("This person gets a discount!")
            System.Console.ReadLine()
        ElseIf passengerAge >= 7 Then
            System.Console.WriteLine("This person DOES NOT get a discount!!!")

这个应用程序的目标是只显示用户是否根据他们的年龄获得折扣,但我需要它来显示他们的全名,它只显示两个字母某些原因?我知道解决这个问题的方法很简单,但我很困惑,究竟要做什么来解决这个问题。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

试试这样:

Dim passengerName As String         ' Passenger's name
Dim passengerAge As Integer         ' Passenger's age

' This is the work done in the housekeeping() procedure 
passengerName = InputBox$("Enter passenger's name: ")

' This is the work done in the detailLoop() procedure 
passengerAge = Convert.ToInt32(InputBox$("Enter passenger's age: "))

' Test to see if this customer is eligible for a 20% discount 
System.Console.WriteLine("{0} {1} a discount!", passengerName, IIf(passengerAge <= 7, "gets", "does not get"))
System.Console.ReadLine()

请注意如何缩短代码并消除冗余的ageString变量。

同样为Keith has pointed out - 请注意,只有7岁以上的乘客才能获得折扣 - 当实际年龄为7时,由于<=7>=7,您的原始逻辑存在缺陷。 / p>

此外,您可以将If Else缩短为几乎一行。