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 天)
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

采纳的回答

VBBV
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 个)

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by