I don't know how to make it repeat 10 times and save all input numbers into text file in a human readable format...Can anyone help me pls?? here's my the question and my code:
2 ビュー (過去 30 日間)
古いコメントを表示
Muhammad Huzayl Rais
2022 年 12 月 11 日
コメント済み: Muhammad Huzayl Rais
2022 年 12 月 13 日
Write a script, which first asks the user to input an integer number between 0 and 255, then
outputs the number to the screen between the characters '<' and '>', and calls a user function
‘mcheck’(as described below)to check the number entered. The input, display and check operation
should be repeated for 10 times. After the 10th iteration, the script should save all the input
numbers to a text file in a human readable format.
The user function ‘mcheck’ mentioned above will do the following check for each number entered:
if the number is in the range of 0 to 255, no change is needed; if the number entered is bigger
than 255, the script should print a message “error: The value too big.” to the screen, but make no
change to the value of the number; if a negative number is entered, no matter what is the exact
number, the script should convert it to -1.
I don't know how to make it repeat 10 times and save all input numbers into text file in a human readable format...Can anyone help me pls??
here's my code:
function mcheck(a)
z=input("Enter A Value Between 0 To 255 = ")
for z=z
disp("<"+z+">")
if z < 0
z = -1
elseif z > 255
disp("error: value too big")
end
end
end
0 件のコメント
採用された回答
VBBV
2022 年 12 月 11 日
編集済み: VBBV
2022 年 12 月 11 日
K = 1:10;
for k = 1:numel(K)
num = randi([-1 256],1); % input('Enter a number between 0 and 255:')
disp("<"+num+">");
Num(k) = mcheck(num);
end
writematrix(Num,'Store_numbers.txt') % store numbers in text file
function num = mcheck(num)
for num=num
if num < 0
num = -1;
elseif num > 255
disp("error: value too big")
end
end
end
7 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
