For some reason my code is not working , can you please help me. Thank you for your time| | *
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 10 de Mzo. de 2014
What difference do you see between what you expect and what you actually get?
Respuestas (2)
Walter Roberson
el 10 de Mzo. de 2014
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
el 10 de Mzo. de 2014
Editada: Patrik Ek
el 10 de Mzo. de 2014
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
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!