I am having an issue with a worksheet online and I was wondering if someone could show me the correct code for my problem and also explain as to why it should be written like that.
Here is the question on the worksheet I am on:
This time let's try a switch statement to convert the characters on a telephone dialpad to the corresponding digits. Recall that
'A', 'B', 'C' map to 2 'D', 'E', 'F' map to 3 'G', 'H', 'I' map to 4 'J', 'K', 'L' map to 5 'M', 'N', 'O' map to 6 'P', 'Q', 'R', 'S' map to 7 'T', 'U', 'V' map to 8 'W', 'X', 'Y', 'Z' map to 9
Write a switch statement that sets the variable digit to the appropriate digit, given the character letter. Set digit to 0 for any other character not listed in the mapping above.
Here is my attempt at solving the issue using a switch statement:
switch (letter)
{
case 'A' || 'B' || 'C' : digit = 2;
break;
case 'D' || 'E' || 'F' : digit = 3;
break;
case 'G' || 'H' || 'I' : digit = 4;
break;
case 'J' || 'K' || 'L' : digit = 5;
break;
case 'M' || 'N' || 'O' : digit = 6;
break;
case 'P' || 'Q' || 'R' || 'S' : digit = 7;
break;
case 'T' || 'U' || 'V' : digit = 8;
break;
case 'W' || 'X' || 'Y' || 'Z' : digit = 9;
break;
default : digit = 0;
break;
}
How should I do this switch statement and what should be corrected??
I get the error message: I found more elements than I expected, here: