总结
sun=0;
i=0;
while(i<=20)
while(j<=5)
y=i+2j
j=j+1;
end
j=0;
i=i+1
end
因错误而失败
当我运行它时,它显示了很多 y=20+i
总结
sun=0;
i=0;
while(i<=20)
while(j<=5)
y=i+2j
j=j+1;
end
j=0;
i=i+1
end
因错误而失败
当我运行它时,它显示了很多 y=20+i
这段代码有很多问题。
下面是一个带有注释的清理版本(即适当的缩进、间距等)
sun = 0; % What's the `sun` doing here? Did you mean `y`?
i = 0;
% Where's the initialisation of j in this list?
% You are using it below uninitialised!
while i <= 20 % Octave/matlab does not need brackets in `while`, `for`,
% `if` statements, etc.
while j <= 5 % Proper indentation clarifies scope of
% outer 'while' block
y = i + 2 * j % Octave/matlab do not support '2j' syntax.
% You need to multiply explicitly.
j = j + 1;
end
j = 0; % Why aren't you resetting this *before* the inner loop?
% How do you think your sum will be affected if `j` is,
% let's say `10`, at the start of this program?
i = i + 1 % Are you leaving the `;` out intentionally here?
% (and `y` above?). If you don't want these values
% printed on the console each time, put a `;` at the end.
end