필터 지우기
필터 지우기

User input while loop

조회 수: 25 (최근 30일)
Chelsea
Chelsea 2012년 1월 29일
답변: Chinmay Anand 2019년 6월 13일
Hi there, I am creating an algorithm to sort screw sizes. I need the user to input the screw size and whether or not to continue (until there are no more screws to sort). I cannot get the while loop to work correctly. I would like to ask the user "Continue? y/n: " and if the user types y, then the loop would continue, and if n, then the loop would stop and display my output. My problem is that I need to ask for the screw size and whether or not to continue every time the loop executes, so it would have to be in the while loop somehow, but that doesn't make sense because I need to initialize the while loop beforehand. Any help would be appreciated!
Thanks,
Chelsea
Virginia Tech

답변 (2개)

Chinmay Anand
Chinmay Anand 2019년 6월 13일
x = []; % This will store screw Sizes
while true
loop = input('Continue? (1/0) : '); % 1 to continue 0 to break loop
if loop == 1
screwSize = input('Enter screw size : ');
x = [x screwSize];
else
break
end
end
sort(x)

Walter Roberson
Walter Roberson 2012년 1월 29일
while true
ask for input
if the input was not 'y'
break
end
ask for a screw size
do whatever
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by