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

1 回表示 (過去 30 日間)
john hollywood
john hollywood 2014 年 3 月 10 日
編集済み: Patrik Ek 2014 年 3 月 10 日
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.
  2 件のコメント
john hollywood
john hollywood 2014 年 3 月 10 日
編集済み: john hollywood 2014 年 3 月 10 日
Hello my code is not work at all . can you please help me, you help will be much appreciated

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


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 Center および File ExchangeJust for fun についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by