Clear Filters
Clear Filters

how to get value for each loop?

1 view (last 30 days)
Evan
Evan on 4 Oct 2023
Commented: Voss on 5 Oct 2023
part1 = 0;
part2 = 0;
part3 = 0;
part4 = 0;
part5 = 0;
a = 1;
b = 2;
c = 3;
d = 4;
Y = 5;
N = 6;
repeat = 5;
i = 0;
while repeat == Y
i = i+1;
part1 = part1+5;
part2 = part2+5;
part3 = part3+5;
part4 = part4+5;
part5 = part5+5;
while part1 < 1 || part1 > 4
part1 = input(['1. Besar gaya gravitasi antara dua massa yang berjarak tertentu satu sama lain adalah \n ' ...
'a.berbanding lurus dengan jarak kedua benda \n ' ...
'b.berbanding terbalik dengan jarak kedua benda \n ' ...
'c.berbanding lurus dengan kuadrat jarak kedua benda \n ' ...
'd.berbanding terbalik dengan kuadrat kedua benda \n']);
if part1==d
fprintf('jawaban anda benar \n')
elseif part1==a || part1 == b || part1 == c
fprintf('jawaban anda salah \n')
else
fprintf('tolong ulangi \n')
end
end
if part1 ==d
score1 = 10;
fprintf('your score %i \n', score1)
else
score1 = 0;
fprintf('your score %i \n',score1)
end
fprintf('\n')
while part2 < 1 || part2 > 4
part2 = input(['2. Kita dapat mengabaikan gaya gravitasi dalam tinjauan partikel elementer. Hal ini karena \n ' ...
'a.pengaruhnya sangat kecil \n ' ...
'b.partikel elementer tidak ada gaya gravitasinya \n ' ...
'c.engaruhnya sangat besar \n ' ...
'd.terlalu sulit diperhitungkan \n']);
if part2 == 1
fprintf('jawaban anda benar \n')
elseif part2 == 2 || part2 == 3 || part2== 4
fprintf('jawaban anda salah \n')
else
fprintf('tolong ulangi \n')
end
end
if part2 ==a
score2 = 10;
fprintf('your score %i \n', score2)
else
score2 = 0;
fprintf('your score %i \n',score2)
end
fprintf('\n')
while part3 < 1 || part3 > 4
part3 = input(['3. Kita dapat mengamati gaya gravitasi saat \n ' ...
'a.benda bergerak aktif \n ' ...
'b.benda yang ditinjau memiliki massa kecil \n ' ...
'c.benda yang ditinjau memiliki massa besar \n ' ...
'd.benda yang ditinjau diam \n']);
if part3 == 3
fprintf('jawaban anda benar \n')
elseif part3==1 || part3==2 || part3==4
fprintf('jawaban anda salah \n')
else
fprintf('tolong ulangi \n')
end
end
if part3 ==c
score3 = 10;
fprintf('your score %i \n', score3)
else
score3 = 0;
fprintf('your score %i \n',score3)
end
fprintf('\n')
while part4 < 1 || part4 > 4
part4 = input(['4. Bila hukum kekekalan energi mekanik untuk suatu sistem berlaku maka \n ' ...
'a.energi potensial selalu bertambah \n ' ...
'b.energi kinetik selalu bertambah \n ' ...
'c.jumlah energi potensial dan energi kinetik selalu berkurang \n ' ...
'd.jumlah energi potensial dan energi kinetik tetap \n']);
if part4==4
fprintf('jawaban anda benar \n')
elseif part4==1 || part4==2 || part4==3
fprintf('jawaban anda salah \n')
else
fprintf('tolong ulangi \n')
end
end
if part4 ==d
score4 = 10;
fprintf('your score %i \n', score4)
else
score4 = 0;
fprintf('your score %i \n',score4)
end
fprintf('\n')
while part5 < 1 || part5 > 4
part5 = input(['5. Besaran yang menyebabkan benda melakukan gerak rotasi adalah \n ' ...
'a.momentum sudut \n ' ...
'b.percepatan sudut \n ' ...
'c.momen gaya \n ' ...
'd.momen inersia \n']);
if part5==4
fprintf('jawaban anda benar \n')
elseif part5==1 || part5==2 || part5==3
fprintf('jawaban anda salah \n')
else
fprintf('tolong ulangi \n')
end
end
if part5 ==d
score5 = 10;
fprintf('your score %i \n', score5)
else
score5 = 0;
fprintf('your score %i \n',score5)
end
if i < 3
repeat = input ('Would you like to repeat? \n');
else
fprintf('end \n')
repeat = N;
end
end
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
im trying to create a multiple choice task, after finishing all 5 multiple choices question, student will be asked "would you like to repeat". they will be given 3 tries. what im trying to do is, after each tries, i would like to store the total score from each loop.
say they used up all 3 tries, total score1 = 30, total socre2 = 40, and total score3 = 10. i would like to know how to get value of the scores from each loop.
thanks

Accepted Answer

Voss
Voss on 4 Oct 2023
% ...
Y = 5;
N = 6;
repeat = 5;
i = 0;
% a vector to store the total scores:
total_scores = [];
while repeat == Y
i = i+1;
% ...
% ask questions, get answers, compute score1, score2, etc.
% ...
% store the current (i-th) total score:
total_scores(i) = score1+score2+score3+score4+score5;
% optionally repeat:
if i < 3
repeat = input ('Would you like to repeat? \n');
else
fprintf('end \n')
repeat = N;
end
end
  2 Comments
Evan
Evan on 5 Oct 2023
Moved: Voss on 5 Oct 2023
thank you, it works
Voss
Voss on 5 Oct 2023
You're welcome!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!