How would I create an input function that asks for 6 unique integers within a finite rage say 1-100 so that is rejects an input other than 6 unique integers within the range and reasks the question.

回答 (2 件)

Stephen23
Stephen23 2020 年 2 月 17 日
編集済み: Stephen23 2020 年 2 月 17 日

0 投票

mnv = 1;
mxv = 100;
vec = [];
while numel(vec)~=6 || any(vec<mnv | vec>mxv) || any(mod(vec,1))
str = input('Enter six integers separated by spaces: ','s');
vec = sscanf(str,'%f',[1,Inf]);
end
And tested:
Enter six integers separated by spaces: 1 2 3 4 5
Enter six integers separated by spaces: 1 2 3 4 5 6.7
Enter six integers separated by spaces: 1 2 3 4 5 6789
Enter six integers separated by spaces: 1 2 3 4 5 6
>> vec
vec =
1 2 3 4 5 6
Bhaskar R
Bhaskar R 2020 年 2 月 17 日
編集済み: Bhaskar R 2020 年 2 月 17 日

0 投票

n_entr = 6;
low_lim = 1;
high_lim = 100;
arr = zeros(1, n_entr);
c = 1;
fl =1;
while fl
in = input('Enter unique value: ');
if in == -1
disp('Program terminated by user !!');
break;
elseif c == n_entr
arr(c) = in;
fl = 0;
disp('All value are taken !!');
elseif any(arr == in)
disp('Duplicate entry, Enter again');
continue;
elseif in>=low_lim & in<=high_lim
arr(c) = in;
c = c+1;
else
disp('Invalid entry, Enter again');
continue;
end
end

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

製品

タグ

質問済み:

2020 年 2 月 17 日

編集済み:

2020 年 2 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by