elseif to switch statement
14 ビュー (過去 30 日間)
古いコメントを表示
I am attempting to switch this into a switch statement.
TestScore = input('Enter a score from the keyboard ')
if(TestScore > 89)
disp('Your Letter grade is A')
elseif(TestScore > 79)
disp('Your Letter grade is B')
elseif(TestScore > 69)
disp('Your Letter grade is C')
elseif(TestScore > 59)
disp('Your Letter grade is D')
else
disp('Your Letter grade is F')
end
0 件のコメント
採用された回答
Adam Danz
2020 年 4 月 20 日
編集済み: Adam Danz
2020 年 4 月 20 日
There's nothing wrong with the if/elseif/else syntax but the conditional statements could be tightened up.
If you prefer a switch-case,
switch true
case (TestScore > 89 && TestScore <=100)
disp('Your Letter grade is A')
case (TestScore > 79 && TestScore <= 89)
disp('Your Letter grade is B')
case (TestScore > 69 && TestScore <= 79)
disp('Your Letter grade is C')
case (TestScore > 59 && TestScore <= 69)
disp('Your Letter grade is D')
case (TestScore >=0 && TestScore <= 59)
disp('Your Letter grade is F')
otherwise
disp('TestScore did not match grade ranges.')
end
2 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で GNC and Avionics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!