please help with this If else statement
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Noah Jacob
am 13 Apr. 2019
Kommentiert: Noah Jacob
am 16 Apr. 2019
clear all
clc
Toefl=input('Toefl=')
Math=input('Mathematic=')
Bio=input('Biologi=')
if(Toefl >= 601) && (Toefl <= 670)
disp ('Excellent')
if(Math >= 601) && (Math <= 670)
disp ('Excellent')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
elseif(Math >= 501) && (Math <= 600)
disp ('Good')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
elseif(Math >= 401) && (Math <= 500)
disp ('Average')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
elseif Math <= 400
disp ('Bad')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
else
disp('Data tidak valid')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
end
elseif (Toefl >= 501) && (Toefl <= 600)
disp ('Good')
elseif (Toefl >= 401) && (Toefl <= 500)
disp ('Average')
elseif Toefl <= 400
disp ('Bad')
else
disp('Data tidak Valid')
end
how to summarize this code
the asked results maybe looks like this
Toefl=602
Toefl =
602
Mathematic=602
Math =
602
Biologi=602
Bio =
602
Excellent
Excellent
Excellent
6 Kommentare
Akzeptierte Antwort
Jan
am 13 Apr. 2019
Bearbeitet: Jan
am 15 Apr. 2019
Similar to your C++ code, just with some shorter names for variables to reduce the chance for typos:
function main
toefl1 = input('masukan toefl1: ');
toefl2 = input('masukan toefl2: ');
toefl3 = input('masukan toefl3: ');
disp(cekNilai(toefl1));
disp(cekNilai(toefl2));
disp(cekNilai(toefl3));
end
function r = cekNilai(n)
if n >= 601 && n <= 670
r = 'excellent';
elseif n >= 501 && n <= 600
r = 'good';
elseif n >= 401 && n <= 500
r = 'avg';
elseif n <= 400
r = 'bad';
else
r = 'invalid input';
end
end
10 Kommentare
Walter Roberson
am 15 Apr. 2019
Yes, other than the change I suggested earlier to
disp(['Your Toefl1 grade is: ', cekNilai(toefl1)])
WIth the single function arranged that way, you only have one "end" statement and so are within the restrictions you have told us about.
Note: if you were to remove the "function main" statement, converting this into a script, then you would need to move the function cekNilai into a separate file in order to maintain the "only one end statement" restriction. Functions that are in scripts need to have "end" statement that matches their "function" line, but functions that are in function-only files do not need that.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!