While loop and arrays

2 ビュー (過去 30 日間)
Tuna KISAAGA
Tuna KISAAGA 2021 年 4 月 23 日
コメント済み: DGM 2021 年 4 月 23 日
I wanted to create a loop and store the values of "secsum" until the value of it is equal to "firstsum" or 7. But it doesn't work :( What do you think is wrong with it?
firstsum = floor(rand*6+1) + floor(rand*6+1);
j=1;
while j>=1
secsum(j) = floor(rand*6+1) + floor(rand*6+1);
if (secsum(j)==firstsum)
fprintf('You WIN with the sequence: %.0f %.0f',firstsum,secsum);
j=0;
elseif (secsum(j)==7)
fprintf('You LOSE with the sequence: %.0f %.0f',firstsum,secsum);
j=0;
else
j=j+1;
end
end
  1 件のコメント
DGM
DGM 2021 年 4 月 23 日
secsum is a vector, so either exit condition will produce multiple lines. If you only want to print the last result of secsum, just do
fprintf('You WIN with the sequence: %.0f %.0f\n',firstsum,secsum(j));
If you want to print the whole secsum vector, you can try using mat2str or some other method to get it into the output string.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by