i have create a program that it will loop back if input y , but if user input other value, still will end the program . how can i restricted the value just y and n

1 view (last 30 days)
CHEE KIN CHUM
CHEE KIN CHUM on 28 Jan 2022
Answered: DGM on 28 Jan 2022
i=0;N='y';
while N=='y'
area=input('Please select area (kitchen, bathroom or storeroom):','s');
k='kitchen';
b='bathroom';
s='storeroom';
switch area
case 'kitchen'
N=input('\nDo you want to reselect area? yes/no \n Enter (y/n)= ','s');
case'bathroom'
case'storeroom'
otherwise
disp('Error is detected. Please select the area again!!!')
end
end
disp('finish')

Answers (1)

DGM
DGM on 28 Jan 2022
This might do what you need.
selectarea = true;
while selectarea
area = input('Please select area (kitchen, bathroom or storeroom): ','s');
switch lower(area)
case 'kitchen'
disp('you selected kitchen')
case'bathroom'
disp('you selected bathroom')
case'storeroom'
disp('you selected storeroom')
otherwise
disp('Error is detected. Please select the area again!!!')
continue;
end
f = input('Do you want to reselect the area? (Y/N): ','s');
selectarea = ~isempty(f) && lower(f(1))=='y';
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!