Someone has created the following code to display the frequency of subway trains
ID: 3630961 • Letter: S
Question
Someone has created the following code to display the frequency of subway trains in a terminal and the names of the train lines are A, B, C, D and E. Rewrite the code using the nested if as a switch structure, but do not change the prompt (You may assume the user enters an upper case letter at the prompt.)Line = input(‘Enter the line.’,’s’)
if Line > ‘A’ & Line < ‘E’
if Line == ‘C’
disp(‘This train arrives every 12 minutes.’)
else
disp(‘This train arrives every 18 minutes.’)
end
else
if Line == ‘A’
disp(‘This train arrives every 24 minutes.’)
else
if Line ~= E
disp(‘This train is not in service.’)
else
disp(‘This train arrives every 30 minutes.’)
end
end
end
Explanation / Answer
Line = input('Enter the line.','s'); switch Line case 'A' disp('This train arrives every 24 minutes.') case {'B', 'D'} disp('This train arrives every 18 minutes.') case 'C' disp('This train arrives every 12 minutes.') case 'E' disp('This train arrives every 30 minutes.') otherwise disp('This train is not in service.') end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.