Write a program to tell whether a person is a Beatle. Here’s the worksheet: The
ID: 3848363 • Letter: W
Question
Write a program to tell whether a person is a Beatle. Here’s the worksheet:
The user types a name in B1, and clicks the button. If the name if John, Paul, George, or Ringo, the program says that the person is a Beatle:
Otherwise, the program shows “Not a Beatle.”
The program should work for upper- and lowercase characters, and when there are spaces before and/or after the name. For example, “ jOHn “ should be identified as a Beatle.
I cannot get my code to work. When I type in a Beatles name it gives me the message "Not a Beatle"
Explanation / Answer
Add the following if-else condition :
If Name.Contains("Beatle") Then
Cells(3,2) = "Thats a Beatle"
Else
Cells(3,2) = " Not a Beatle "
End If
For the first problem(i.e case sensitivity )
Use String.Compare method to comapre 2 strings using False option to ignore case.
Use String.Trim() to remove leading and trailing spaces
Name = Trim(Name)
Cells(3,2) = "Thats a Beatle"
ElseIf String.Compare(Name,"Paul", False) Then
Cells(3,2) = "Thats a Beatle"
Cells(3,2) = "Thats a Beatle"
Cells(3,2) = "Thats a Beatle"
ElseIf Name.Contains("Beatle") Then
Cells(3,2) = "Thats a Beatle"
Else
Cells(3,2) = " Not a Beatle "
End If
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.