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

조회 수: 7 (최근 30일)
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

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 13일
while ~ismember(cube_choice_avg_time, 1:3)

추가 답변 (1개)

Sharareh J
Sharareh J 2021년 2월 12일
Write [1 2 3] instead of 1;2;3

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by