For some reason my code is not working , can you please help me. Thank you for your time| | *

조회 수: 1 (최근 30일)
function mathgame = mathgame()
disp('Welcome to the math game!');
count = 1;
correct = 0;
incorrect = 0;
rand1 = 1 + 9 * rand(1);
rand2 = 1 + 9 * rand(1);
sprintf(%d: %d + %d =",count,rand1,rand2)
answer = input('');
if answer == rand1+rand2
disp('Correct!');
correct = correct +1;
else
disp('Incorrect!');
incorrect = incorrect+1;
end
again = input('Again','s');
while strcmp(again,'y')==1
count = count+1;
rand1 = 1 + 9 * rand(1);
rand2 = 1 + 9 * rand(1);
sprintf(%d: %d + %d =",count,rand1,rand2);
answer = input('');
if answer == rand1+rand2
disp('Correct!');
correct = correct +1;
else
disp('Incorrect!');
incorrect = incorrect+1;
end
again = input('Again','s');
end
sprintf('Correct: %d (%.2f %%), Incorrect %d (%.2f %%)',correct,(100.0*correct/count),incorrect,(100.0*incorrect/count))
end
mathgame();
  • | |
  • * For some reason my code is not working , can you please help me. Thank you for your time| | *
  댓글 수: 2
Walter Roberson
Walter Roberson 2014년 3월 10일
What difference do you see between what you expect and what you actually get?
john hollywood
john hollywood 2014년 3월 10일
my code is not even running Sir , that what i am getting when tried it. " >> mathgame() Error: File: mathgame.m Line: 8 Column: 46 Expression or statement is incorrect--possibly unbalanced (, {, or [."

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

답변 (2개)

Walter Roberson
Walter Roberson 2014년 3월 10일
You have
sprintf(%d: %d + %d =",count,rand1,rand2)
Strings in MATLAB need to be enclosed in apostrophes. You have missed the leading indicator that a string is following, and you ended the string with double-quote.

Patrik Ek
Patrik Ek 2014년 3월 10일
편집: Patrik Ek 2014년 3월 10일
Walter Roberson is correct in his answer. The syntax for sprintf is incorrect. However, you want to use a random integer, or at least a number with less than or equal to 4 decimals. This since matlab only displays 4 decimals without format long, for not mentioning that the game will be really iritating woth to many decimals. The modification in the program concerning this is that I have used randi instead of rand.
The other problem is sprintf. This functions returns a string as a variable. I have made changes in the code to use fprintf instead, which displays a test string (this function can also save text to file, but I have not done it since you did not ask to store the answers on file). I have also made changes to add newline at the end of each string (\n).
The third issue is that you have used '%d' for format specifier, This is not good! You have not defined your variables rand 1 and rand2 as integers, but as doubles, for creating integers you need something like:
randInt = int64(1+9*rand(1));
but I have used randi instead, since I saw now real need to use integers here. However I changed the format specifier to '%g', which takes a float and displays it without trailing zeros.
Except these issues, which I would consider minor, even though the text is worth reading through, Everything is correct.
Ok here comes the code!
function mathgame()
disp('Welcome to the math game!');
count = 1;
correct = 0;
incorrect = 0;
rand1 = randi([1 10],1);
rand2 = randi([1 10],1);
fprintf('%g: %g + %g = ''\n',count,rand1,rand2)
answer = input('');
if answer == rand1+rand2
disp('Correct!');
correct = correct +1;
else
disp('Incorrect!');
incorrect = incorrect+1;
end
again = input('Again ','s');
while strcmp(again,'y')==1
count = count+1;
rand1 = randi([1 10],1);
rand2 = randi([1 10],1);
fprintf('%g: %g + %g = "\n',count,rand1,rand2);
answer = input('');
if answer == rand1+rand2
disp('Correct!');
correct = correct +1;
else
disp('Incorrect!');
incorrect = incorrect+1;
end
again = input('Again ','s');
end
fprintf('Correct: %g (%.2f %%), Incorrect %g (%.2f %%)\n',correct,(100.0*correct/count),incorrect,(100.0*incorrect/count))
end

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by