Info

此问题已关闭。 请重新打开它进行编辑或回答。

help please Need help making a script!!! any help?please

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| | *

回答(1 个)

Chandrasekhar
Chandrasekhar 2014-3-10
编辑:Chandrasekhar 2014-3-10
Please check if this satisfies your requirement
function mathgame = mathgame()
clc;
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('guess the result of the sum of random numbers: ');
sum = rand1+rand2;
if answer == sum
disp('Correct!');
correct = correct +1;
else
disp('Incorrect!');
incorrect = incorrect+1;
end
sprintf('Correct: %d (%.2f %%), Incorrect %d (%.2f %%)',correct,(100.0*correct/count),incorrect,(100.0*incorrect/count))
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('guess the result of the sum of random numbers: ');
sum = rand1+rand2;
if answer == sum
disp('Correct!');
correct = correct +1;
else
disp('Incorrect!');
incorrect = incorrect+1;
end
again = input('Again? ','s');
sprintf('Correct: %d (%.2f %%), Incorrect %d (%.2f %%)',correct,(100.0*correct/count),incorrect,(100.0*incorrect/count))
end
end

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by