Add code to have switch repeat if a case is not met

3 次查看(过去 30 天)
fprintf('Difficulty level: \nEasy: [0, 10] \nMedium: [0, 50] \nHard: [0, 100] \n'); % Print statement outputting different difficulties
diff_level = input('Choose difficulty level: ', 's'); % user input for which difficulty desired, 's' is present since input is string
numplayer = input('How many players: '); % user input for number of players
leaderboard = cell(numplayer,2); % Stores result of guesses
switch diff_level % Switch case to assign difficulties with ranges
case 'Easy'
max_number = 10;
case'easy'
max_number = 10;
case'EASY'
max_number = 10;
case 'Medium'
max_number = 50;
case'medium'
max_number = 50;
case'MEDIUM'
max_number = 50;
case 'Hard'
max_number = 100;
case'hard'
max_number = 100;
case'HARD'
max_number = 100;
otherwise
disp('Difficulty not available.')
end
for player = 1:numplayer
number_of_attempt = 1;
rng('shuffle') % Random number generator seeded to time
number_to_guess = randi([0, max_number]); % Creates random # from 0 to max number set by difficulty
fprintf('\nPlayer Ready\n')
player_guess = input('Take a guess: ');
while player_guess ~= number_to_guess % While loop to run untill correct # is guessed
if player_guess < number_to_guess % if statement for low guess
disp('Too low! guess again:')
end
if player_guess > number_to_guess % If statement for high guess
disp('Too high! guess again:')
end
number_of_attempt = number_of_attempt + 1;
player_guess = input('Take a guess: ');
end
leaderboard{player, 1} = number_of_attempt; % adds # of guesses to leaderboard
leaderboard{player, 2} = input('Enter player name: ', 's'); % assigns name to that persons attempt
end
leaderboard = sortrows(leaderboard,1); % sort the table
position = 0;
fprintf('-----------------------------RESULTS--------------------------------\n')
for player = 1:numplayer
position = player;
if player > 1 % reduce position for draw
if leaderboard{player, 1} == leaderboard{player-1, 1} % Orders playes by guesses
position = position - 1;
end
end
fprintf('%d place %s with %d attempts. \n', position, leaderboard{player,2}, leaderboard{player,1}) % print statement to output order of players
end
When a case isnt met the code ends and has an error. How can I instead have the code continue to run and repeat the switch if a case isn't met?

回答(3 个)

Steven Lord
Steven Lord 2022-9-21
You can simplify your switch call so you don't have to include 2^(number of letters) cases each with a different combination of cases. The lower function converts text into all lower case.
E = 'EaSY';
switch lower(E)
case 'easy'
disp('Easy case selected')
case 'medium'
disp('Medium case selected')
otherwise
disp('No match')
end
Easy case selected
What happens if I remove the lower call?
E = 'EaSY';
switch E
case 'easy'
disp('Easy case selected')
case 'medium'
disp('Medium case selected')
otherwise
disp('No match')
end
No match
I'd probably assign max_number some easily detected "invalid" value and wrap your switch call in a while loop that tests if max_number is still in that invalid state.
N = NaN;
while isnan(N)
x = randi(10); % Random integer between 1 and 10
fprintf("Generated %d.\n", x)
if x >= 9
N = x; % Break out of the loop as soon as 9 or 10 is generated
end
end
Generated 7. Generated 1. Generated 7. Generated 9.
disp(N)
9
While I used randi to generate the data that decides whether or not to exit the while loop you could call input and switch.

Jan
Jan 2022-9-21
编辑:Jan 2022-9-21
knownLevels = {'easy', 'medium', 'hard'};
Levels = [10, 50, 100];
fprintf('\nChoose one of the known levels: %s\n', strjoin(knownLevels, ', '));
max_number = 0;
while max_number == 0
diff_level = input('Choose difficulty level: ', 's');
match = strcmpi(diff_level, knownLevels);
if any(match)
max_number = Levels(match);
else
fprintf('\nChoose one of the known levels: %s\n', strjoin(knownLevels));
end
end

Eric Sofen
Eric Sofen 2022-9-21
A while loop is one way to accomplish this. Also note that you can use lower() to reduce the number of cases.
max_number = nan;
while isnan(max_number)
fprintf('Difficulty level: \nEasy: [0, 10] \nMedium: [0, 50] \nHard: [0, 100] \n'); % Print statement outputting different difficulties
diff_level = input('Choose difficulty level: ', 's'); % user input for which difficulty desired, 's' is present since input is string
numplayer = input('How many players: '); % user input for number of players
switch lower(diff_level) % Switch case to assign difficulties with ranges
case'easy'
max_number = 10;
case'medium'
max_number = 50;
case'hard'
max_number = 100;
otherwise
disp('Difficulty not available. Please specify game parameters again')
% don't set max_number
end
end
% ...

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by