Switch Case with a range of letters?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
furcifer
el 13 de Dic. de 2018
Respondida: Steven Lord
el 13 de Dic. de 2018
OK, I'm just playing with some code. Here's the code:
clear, clc;
while 1
n = input('Type a Word Starting With a Capital Letter: ','s');
n=n(1);
switch n
case 'A'
disp('An A word!')
break;
case 'B'
disp('A B word!')
break;
case 'C'
disp('A C word!')
break;
case 'D' <= 'Z'
disp("try using A,B or C this time")
otherwise
disp('incorrect word, I said use a CAPITAL!')
end
end
I added the "while true" to keep the program running until one of my cases is met. I'm trying to use a range from "D" to "Z" but it doesn't seem to work. I thought the characters were seen as an integer in programming so I'm not sure why it can't evaluate the case?
0 comentarios
Respuesta aceptada
Walter Roberson
el 13 de Dic. de 2018
case num2cell('D':'Z')
... Tested. It works.
3 comentarios
Walter Roberson
el 13 de Dic. de 2018
while 1
n = input('Type a Word Starting With a Capital Letter: ','s');
n=n(1);
switch true
case n == 'A'
disp('An A word!')
break;
case n == 'B'
disp('A B word!')
break;
case n == 'C'
disp('A C word!')
break;
case n >= 'D' && n <= 'Z'
disp("try using A,B or C this time")
otherwise
disp('incorrect word, I said use a CAPITAL!')
end
end
Más respuestas (2)
Image Analyst
el 13 de Dic. de 2018
Try this:
maxIterations = 20; % Whatever. A failsafe
loopCounter = 1;
while loopCounter <= maxIterations
n = input('Type a Word Starting With a Capital Letter: ','s');
n = n(1); % Extract first letter only
switch n
case 'A'
disp('An A word!')
break; % Quit look if they type an A word.
case 'B'
disp('A B word!')
break; % Quit look if they type a B word.
case 'C'
disp('A C word!')
break; % Quit look if they type a C word.
case {'D', 'E', 'F', 'G', 'Z'} % Add all the others up to 'Z'
uiwait(helpdlg("Try using A,B or C this time"))
otherwise
disp('Incorrect word, I said use a CAPITAL!')
end
loopCounter = loopCounter + 1;
end
Steven Lord
el 13 de Dic. de 2018
I made a couple modifications to your code.
foundCapitalLetter = false;
Rather than using while 1, I wanted to use something that immediately explains to anyone reading the code what's going on. At the start of the code, we haven't found a capital letter word yet so this variable starts off as false.
while ~foundCapitalLetter
If we enter or continue in the while loop, we haven't yet found a capital letter.
str = input('Type a Word Starting With a Capital Letter: ','s');
n = str(1);
I'm guessing you may want to do something with the word after the while loop so rather than throwing away all but the first letter I kept it around in a different variable.
switch n
case 'A'
disp('An A word!')
foundCapitalLetter = true;
Success! We keep track of the fact that we succeeded by setting foundCapitalLetter to true. This will break us out of the while loop when we next get back to the top.
case 'B'
disp('A B word!')
foundCapitalLetter = true;
case 'C'
disp('A C word!')
foundCapitalLetter = true;
otherwise
if ismember(n, 'D':'Z')
MATLAB knows how to make a sequence of letters using the colon operator. I don't know if you've learned the ismember function yet, but I think it's a more commonly used function than num2cell and so you may be okay using it. If not, you can use Walter's "n >= 'D' && n <= 'Z'" here.
disp("try using A,B or C this time")
We found a capital letter, but not one of the ones you'd accept, so foundCapitalLetter remains false.
else
disp('incorrect word, I said use a CAPITAL!')
No capital letter, try again. foundCapitalLetter remains false.
end
end
end
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!