Using while statements to create a loop if a value entered is not correct

7 vues (au cours des 30 derniers jours)
So im writing a code right now which prompts the user to enter the a number from 1-3. ive created a while statement which creates a loop and prompts them to type the value in again untill its either 1 2 or 3. However when i run the code and type in 2 for example, which is a correct number, its staying in the loop as if ive entered a wrong number. The only number which gets me out of the loop is when i use the number one. I feel like my while statement is incorrect, Here is the code: (ignore the MaxT and MinT)
case 1
MaxT = 0; MinT = 0;
disp('You have chosen average time to complete')
prompt = ['Please type which cube you would like\n<strong>Option 1:' ...
'</strong>2x2\n<strong>Option 2:</strong>3x3\n<strong>Option 3:</strong>4x4'];
cube_choice_avg_time = input(prompt);
while cube_choice_avg_time ~= 1;2;3;
prompt = ['You need to enter a value of 1,2 or 3\nPlease type ' ...
'which cube you would like\n<strong>Option 1:</strong>2x2\n<strong>Option 2:' ...
'</strong>3x3\n<strong>Option 3:</strong>4x4'];
cube_choice_avg_time = input(prompt);
end
if cube_choice_avg_time == 1
Full_cube_data = xlsread("Full_Cube_data.xlsx");
specific_data = Full_cube_data(73:108);
Avg = mean(specific_data);
disp(['Average Time (s) = ', num2str(Avg)])
elseif cube_choice_avg_time == 2
Full_cube_data = xlsread("Full_Cube_data.xlsx");
specific_data = Full_cube_data(37:72);
Avg = mean(specific_data);
disp(['Average Time (s) = ', num2str(Avg)])
elseif cube_choice_avg_time == 3
Full_cube_data = xlsread("Full_Cube_data.xlsx");
specific_data = Full_cube_data(1:36);
Avg = mean(specific_data);
disp(['Average Time (s) = ', num2str(Avg)])
end

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Fév 2021
while ~ismember(cube_choice_avg_time, 1:3)

Plus de réponses (1)

Sharareh J
Sharareh J le 12 Fév 2021
Write [1 2 3] instead of 1;2;3
  2 commentaires
Cizzxr
Cizzxr le 12 Fév 2021
Thanks a lot! New to this stuff!
Sharareh J
Sharareh J le 12 Fév 2021
Glad it helped you!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by