フィルターのクリア

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

5 ビュー (過去 30 日間)
Cizzxr
Cizzxr 2021 年 2 月 12 日
回答済み: Walter Roberson 2021 年 2 月 13 日
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
  2 件のコメント
Cizzxr
Cizzxr 2021 年 2 月 12 日
Thanks a lot! New to this stuff!
Sharareh J
Sharareh J 2021 年 2 月 12 日
Glad it helped you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by