Yes or No question using while loop

27 ビュー (過去 30 日間)
Danny Allen
Danny Allen 2021 年 2 月 2 日
コメント済み: Danny Allen 2021 年 2 月 2 日
I'm trying input values to find area, and then ask a yes or no question to test more values afterwards. However, I'm having a hard time incorporating the while loop with the yes or no statement. Any help is appreciated.
base = input('enter base of beam');
height = input('enter height of beam');
A = base*height
disp(['The area of the beam is= ', num2str(A)])
choice = questdlg('would you like to try other values?',...
'Choose',...
%the 'No' button does not appear, am I using it incorrectly?
'Yes','No');
%See if the user would like to try more values:
% Read new data
%Starting the while loop here is where I have a problem, even if I assigned a random value to 'yes' or 'no' - doesn't work
switch choice
case 'Yes'
base = input('enter base of beam');
height = input('enter density of beam');
A = base*height
disp(['The area of the beam is= ', num2str(A)])
% Repeat until user chooses 'No'
case 'No'
disp('YAY! You finished you calculation!')
end %switch
  2 件のコメント
Ive J
Ive J 2021 年 2 月 2 日
You forgot default answer.
choice = questdlg('would you like to try other values?',...
'Choose',...
'Yes','No','No');
Danny Allen
Danny Allen 2021 年 2 月 2 日
Thank you very much! I didn't realize I had to apply the default answer again

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

採用された回答

David Hill
David Hill 2021 年 2 月 2 日
while 1
base = input('enter base of beam');
height = input('enter height of beam');
A = base*height
disp(['The area of the beam is= ', num2str(A)])
choice = questdlg('would you like to try other values?','Choose','Yes','No','No');
if isequal(choice,'No')
break;
end
end
disp('YAY! You finished you calculation!');
  1 件のコメント
Danny Allen
Danny Allen 2021 年 2 月 2 日
Thank you so much for helping me with this! Looking back at your alterations and referring back to matlab using the help feature, has guided me to a better understanding of how to execute and terminate a while loop.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by