Confirm that the input is a positive integer and not an array. I got this far, but when I type in [1 2 3] is does not prompt to enter in the positive integer again.
n = input('Enter in a positive integer: ')
while (n<0) & (isvector(n))
n = input('Enter in a positive integer: ')
end

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 21 日
編集済み: Azzi Abdelmalek 2016 年 3 月 21 日

0 投票

n = input('Enter in a positive integer: ')
while n<0 | numel(n)>1 | fix(n)~=n
n = input('Enter in a positive integer: ')
end

1 件のコメント

Walter Roberson
Walter Roberson 2016 年 3 月 21 日
Test the number of elements first. "n<0" is false if you have a vector that contains a mix of negative and non-negative values.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by