In: Computer Science
Matlab code
Here is the command that asks the user if he/she wants to play again:
playAgain = input(Would you like to play again? (y/n): ', 's');
Your task is to write a validation loop that only strings that start with a letter "y" or a letter "n", irrespective of cases, are considered to be valid input.
Program:
clc;
playAgain = input('Would you like to play again? (y/n): ', 's'); % Accept input
while(ne(playAgain,'Y') && ne(playAgain,'y') &&
ne(playAgain,'N') && ne(playAgain,'n')) % validate the
input is correct or not
disp('Not a valid choice'); % it print not a valid choice
playAgain = input('Would you like to play again? (y/n): ', 's'); %
Accept input
end
disp('Valid Input!') % print valid input
Output: