for loop for an interactive program

조회 수: 3 (최근 30일)
Abhishek
Abhishek 2022년 10월 27일
댓글: Walter Roberson 2022년 10월 31일
Hi Guys, I have got the foloowing task, so far I have created few parts but not been able to do input of variable part.
Interactive Program: Write a program that measures a person's reaction time when measures keystrokes. The rand and round commands are intended to give the user an integer between 0 and 10, which he should immediately type on the keyboard, while the
elapsed time is measured [tic, toc]. The procedure should be repeated ten times. The mean value of the reaction time (between showing and entering) is displayed as a result in a separate window [msgbox]. If the person makes an incorrect entry, it must program can be canceled with a message.Expand the program so that the following are dependent on the reaction time (mean value).
Messages are displayed:
T ≤ 0.5 S ek You are very fast!!
T ≤ 1 S ek You are fast !!
T ≤ 2 sec Not very impressive !!
T > 2 sec A turtle would be faster than you!!
tic
n=10
for x= 1:n
x=randi([1 10],1,1)
msgbox("x")
input="Bitte Zahl eingeben"
end
%if t<=0,5
disp("Du bist sehr schnell !!")
%else 0.5<t>=1
disp("Du bist schnell !!")
%elseif 1<t<=2
disp("Nicht sehr beeindruckened!!")
%elseif t>2
disp("Eine Schildkröte wäre schneller als du !!")

답변 (1개)

Shoaib iqbal
Shoaib iqbal 2022년 10월 31일
Hi Abhishek
I understand that you are trying to measure a person’s reaction time using a task you have created.
To measure the elapsed time between showing and entering the integer value, you can use tic toc. Following code snippet may solve your problem.
for i = 1:2
x = randi(10);
tic
display(x);
prompt = "input ";
y = input(prompt);
t = toc;
if (y == x)
if (t<=0.5)
disp("you are very fast");
elseif (t>0.5 && t<=1)
disp("you are fast");
elseif (t>1 && t<=2)
disp("Not very impressive");
else
disp("A turtle would be faster than you");
end
else
disp("wrong value entered")
end
end
Hope this is helpful.
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 10월 31일
This was a homework assignment, so we discourage providing complete solutions

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by