필터 지우기
필터 지우기

using menu() function to calculate time differences

조회 수: 2 (최근 30일)
Ella Yeo
Ella Yeo 2019년 3월 3일
답변: Sreelakshmi S.B 2019년 3월 6일
london = datetime
timeZones = {'Amsterdam', 'Tokyo', 'Canberra', 'Los Angeles', 'New York', 'Local (London)','Close'}
while button~=7 %7th button is 'close'
button == menu('Choose your timezones',timeZones)
%Amsterdam
if button ==1
ams=london+hours(1)
disp(ams)
disp('+1:00')
%Tokyo
elseif button ==2
tok=london+hours(9)
disp(tok)
disp('+9:00')
%Canberra
elseif button ==3
can= london +hours(11)
disp(can)
disp('+11:00')
%Los Angeles
elseif button==4
los = london +hours(-8)
disp(los)
disp('-8:00')
%New York
elseif button==5
new= london +hours(-5)
disp(new)
disp('-5:00')
%London(Local)
elseif button==6
disp('No time difference')
disp(london)
else
end
end
I used a while loop so the menu doesn't close after a choice.
it does work in that sense, but when i press 'close' i cannot close it and the loop becomes endless unless i force to end. Also no matter what button i press, it gives london time.
what have I done wrong here?

답변 (1개)

Sreelakshmi S.B
Sreelakshmi S.B 2019년 3월 6일
There's a mistake in the 5th line of you code.You're using == .You need to use =.
Also,you need to have the first assignment to 'button' before comparing it in the while condition.The code below works fine.I just made a few modifications to your code:
london = datetime;
timeZones = {'Amsterdam', 'Tokyo', 'Canberra', 'Los Angeles', 'New York', 'Local (London)','Close'};
button = menu('Choose your timezones',timeZones);
while button~=7 %7th button is 'close'
%Amsterdam
if button ==1
ams=london+hours(1);
disp(ams);
disp('+1:00');
%Tokyo
elseif button ==2
tok=london+hours(9);
disp(tok);
disp('+9:00');
%Canberra
elseif button ==3
can= london +hours(11);
disp(can);
disp('+11:00');
%Los Angeles
elseif button==4
los = london +hours(-8);
disp(los);
disp('-8:00');
%New York
elseif button==5
new= london +hours(-5);
disp(new);
disp('-5:00');
%London(Local)
elseif button==6
disp('No time difference');
disp(london);
else
end
button = menu('Choose your timezones',timeZones);
end

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by