Using while statements to create a loop if a value entered is not correct
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cizzxr
am 12 Feb. 2021
Beantwortet: Walter Roberson
am 13 Feb. 2021
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
Akzeptierte Antwort
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!