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!


