using menu() function to calculate time differences
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposte (1)
Sreelakshmi S.B
il 6 Mar 2019
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
0 Commenti
Vedere anche
Categorie
Scopri di più su Oceanography and Hydrology in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!