Thursday, February 20, 2014

Section 4.3: Select Case Blocks




40503:  Write a Select Case statement that tests teh value of the Char variable response and performs the following actions:  if response is y, the message "Your request is being processed" is printed.  if response is n, the message "Thank you anyway for your consideration" is printed.  if response is h, the message "Sorry, no help is currently available is printed.  for any other value of response, the message "Invalid entry; please try again" is printed.

Select Case response
Case "y"
System.Console.Writeline("Your request is being processed")
Case "n"
System.Console.Writeline("Thank you anyway for your consideration")
Case "h"
System.Console.Writeline("Sorry, no help is currently available")
Case Else
System.Console.Writeline("Invalid entry; please try again")
End Select

40018:  Write a Select Case statement that tests the value of the integer variable semesterAverage and assigns a grade to the string variable grade based upon the usual criteria (i.e., greater than or equal to 90 is an A, between 80 and 89 inclusively is a B, etc).  If the average falls outside the range of 0 and 100, grade should be assigned the empty string and the message "Invalid semester average" should be displayed in a message box.

Select Case semesterAverage
Case 90 to 100
grade = ("A")
Case 80 to 89
grade = ("B")
Case 70 to 79
grade = ("C")
Case 60 to 69
grade = ("D")
Case 0 to 59
grade = ("F")
Case Else
MessageBox.Show("Invalid semester average")
grade = ("")
End Select




41121:  Given an int variable named yesCount and another int variable named noCount and a char variable named response, write the necessary code to read a value from the text box textBox into response and then carry out the following:  if the character typed in is a y or a Y then increment yesCount and print out "YES WAS RECORDED".  if the character typed in is an n or an N then increment noCount and print out "NO WAS RECORDED".  If the input is invalid just print the message "INVALID" and do nothing else.

response = textBox.Text
Select Case response
Case "y"
yesCount += 1
System.Console.Writeline("YES WAS RECORDED")
Case "Y"
yesCount += 1
System.Console.Writeline("YES WAS RECORDED")
Case "n"
noCount += 1
System.Console.Writeline("NO WAS RECORDED")
Case "N"
noCount += 1
System.Console.Writeline("NO WAS RECORDED")
Case Else
System.Console.Writeline("INVALID")
End Select

2 comments: