Why is my nested if statement is not working.

1 回表示 (過去 30 日間)
Lou
Lou 2021 年 8 月 1 日
コメント済み: Lou 2021 年 8 月 1 日
fid = fopen('studentData.txt', 'r');
while (~feof(fid))
str = fgetl(fid);
T = textscan(str,'%s %s %f %f %f %f %f %f %f %f');
disp(str);
Total = (T{1,3}+T{1,4}+T{1,5}+T{1,6}+T{1,7}+T{1,8}+T{1,9}+T{1,10})
Average = Total/10
mark1 = T{1,4};
mark2 = T{1,6};
mark3 = T{1,8};
mark4 = T{1,10};
if (mark1 >= 90)
disp('A+');
elseif (80 <= mark1 < 90)
disp('A');
if (70 <= mark1 < 80)
disp('A-');
elseif (60 <= mark1 < 70)
disp('B');
if (50 <= mark1 < 60)
disp('C');
elseif (40 <= mark1 < 50)
disp('D');
if (35 <= mark1 < 40)
disp('E');
else
disp('F');
end
end
end
end
end
fclose(fid)
  1 件のコメント
Rik
Rik 2021 年 8 月 1 日
編集済み: Rik 2021 年 8 月 1 日
Your indentation seems to suggest you expect different behavior.
Have you tried setting a breakpoint at the start of your function and going through your code line by line?
Edit:
I just noticed you're doing this:
(80 <= mark1 < 90)
What do you expect to happen? There is an orange line under it in the editor. Don't ignore it.

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

採用された回答

Image Analyst
Image Analyst 2021 年 8 月 1 日
In addition to Rik's comment above, you forgot to read the posting guidelines
which means you never learned that you were supposed to attach 'studentData.txt' to make it easy for us to run your code and help you. But anyway, try it this way:
fid = fopen('studentData.txt', 'rt');
lineCounter = 0;
while (~feof(fid))
str = fgetl(fid);
lineCounter = lineCounter + 1;
T = textscan(str,'%s %s %f %f %f %f %f %f %f %f');
fprintf('\nRead line #%d : "%s".\n', str);
Total = (T{1,3}+T{1,4}+T{1,5}+T{1,6}+T{1,7}+T{1,8}+T{1,9}+T{1,10})
Average = Total/10
mark1 = T{1,4};
mark2 = T{1,6};
mark3 = T{1,8};
mark4 = T{1,10};
if (mark1 >= 90)
fprintf('A score of %.1f is A+.\n', mark1);
elseif mark1 >= 80
fprintf('A score of %.1f is A.\n', mark1);
elseif mark1 >= 70
fprintf('A score of %.1f is A-.\n', mark1);
elseif mark1 >= 60
fprintf('A score of %.1f is B.\n', mark1);
elseif mark1 >= 50
fprintf('A score of %.1f is C.\n', mark1);
elseif mark1 >= 40
fprintf('A score of %.1f is D.\n', mark1);
elseif mark1 >= 35
fprintf('A score of %.1f is E.\n', mark1);
else
fprintf('A score of %.1f is F.\n', mark1);
end
end
fclose(fid)
Note that you don't need to check in a range between a high number and a low number because simply checking if it's above this high number means it will go in that one and skip all the rest. Think about it -- why check for the low number of the range??? You don't need to.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 1 日
elseif (80 <= mark1 < 90)
As far as MATLAB is concerned, that means the same as
elseif all((80 <= mark1) < 90, 'all')
That is, 80 <= mark1 is calculated, returning 0 (false) or 1 (true), and that 0 or 1 is then to be compared to 90 .
It does not mean to mark1 should be tested to be between 80 (inclusive) and 90 (exclusive). MATLAB itself does not support that kind of distributed comparison (there is one specific Symbolic Toolbox function that does support those kind of comparisons)
You need to instead
elseif 80 <= mark1 && mark1 < 90
See though Image Analyst's post showing that you do not need to test the upper bounds if you code right.
  1 件のコメント
Lou
Lou 2021 年 8 月 1 日
Thank you all

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

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by