MATLAB Answers

While loop not working

39 ビュー (過去 30 日間)
NAMRATA SINGH
NAMRATA SINGH 2016 年 11 月 24 日
コメント済み: Jan 2016 年 11 月 24 日
Hello everyone,
I am trying to work on this part on it stops at second section with no error message.
while j <9
if j<1
i = 'E';
set(figure('units','normalized','outerposition',[0 0 1 1], 'color', [1 1 1]))
axis off
text(.45, .6, 'E', 'fontname', 'Times', 'fontsize', 200)
pause
LetterOne = input('What letter did you see on the screen?', 's')
LetterOne = upper(LetterOne)
if ~isempty(LetterOne)
LetterOne(LetterOne == ' ') = [];
disp('Press a key to continue')
else disp('Thank you participating in the vision test.')
disp('Your vision is 20/200');
return
end
clf, close
end
if j<2
i= 'F P';
set(figure('units','normalized','outerposition',[0 0 1 1], 'color', [1 1 1]))
axis off
text(.40, .6, 'F P', 'fontname', 'Times', 'fontsize', 100)
pause
LetterTwo = input('What letter did you see on the screen?', 's')
LetterTwo = upper(LetterTwo)
if ~isempty(LetterTwo)
LetterTwo(LetterTwo == ' ') = [];
disp('Press a key to continue')
else disp('Thank you participating in the vision test.')
disp('Your vision is 20/100')
return
end
clf, close
end
if j<3
i = 'T O Z';
set(figure('units','normalized','outerposition',[0 0 1 1], 'color', [1 1 1]))
axis off
text(.45, .6, 'T O Z', 'fontname', 'Times', 'fontsize', 70)
pause
LetterThree = input('What letter did you see on the screen?', 's')
LetterThree = upper(LetterThree)
if ~isempty(LetterThree)
LetterThree(LetterThree == ' ') = [];
disp('Press a key to continue')
else disp('Thank you participating in the vision test.')
disp('Your vision is 20/70');
return
end
end

回答 (1 件)

Daniel kiracofe
Daniel kiracofe 2016 年 11 月 24 日
I see two issues with your code:
1) By virtue of the fact that you have "while j <9" at the top, this implies that you expect that j will be changing somewhere inside the loop. But, as far as I can tell, j never changes. Therefore your loop is likely to execute an infinite number of time, or zero, depending on the initial value of j. You probably want to have a "j=j+1" somewhere in your loop, or maybe use "for j=1:9" instead.
2) You said the code stops at the second part, so I'm guessing that j starts out < 9, and that you expected it to continue. You have a "return" in the middle of the loop. Guess what return does? It stops the execution of the code right then and returns to the calling subroutine. If you want the code to keep going, just delete the return statement.
  1 件のコメント
Jan
Jan 2016 年 11 月 24 日
When "j" is not defined anywhere, it is the imaginary unit.

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

Community Treasure Hunt

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

Start Hunting!

Translated by