Info
此问题已关闭。 请重新打开它进行编辑或回答。
how can i determine why my code is accepting a non integer when it's not suppose to
1 次查看(过去 30 天)
显示 更早的评论
I'm solving problem that is supposed to only accept an integer as input but its accepting a non integer. I'm attaching the problem and the code i wrote . please help me check what could be wrong thanks
0 个评论
回答(2 个)
Roger Stafford
2016-6-11
编辑:Roger Stafford
2016-6-11
Your instructions specified that if the input is invalid, you were to return an empty string. This you did not do. Instead your code proceeds just as if the input is valid. When you do an ‘if’ operation terminated by ‘end’, the code proceeds from the point after the ‘end’ unless a ‘break’ or 'continue' has been called. There is no ‘break’ or 'continue' in your code.
Also the test for integer is wrong, in any case. What ‘isinteger’ tests for is whether the input is of TYPE integer which is a different thing from a floating point number being an integer.
In addition you were supposed to test that the input integer does not exceed 3000, otherwise it is invalid.
4 个评论
Roger Stafford
2016-6-13
You can only use the "short-circuit" logical operators on scalars, not vectors. Use | and & instead for vectors.
Image Analyst
2016-6-11
Try something like this:
if y - int32(y) == 0
% It's an integer.
else
% It's not an integer
message = sprintf('%f is not an integer', y);
uiwait(errordlg(message));
cent = []; % Return null.
return;
end
6 个评论
Image Analyst
2016-6-12
Your directions were not clear. With 2 numbers, you could have returned 2 numbers, or you could have returned null or some other error condition. Apparently your robo-grader wanted you to just return null if you passed it two numbers because that's what you did and it accepted it.
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!