How to form an array with the input values

%How would i form an array if i had 5 inputs like this of random number
% displayed at the end.
clc,clear
x = 12;
num = 0;
for index = 1:5
y = input('Guess the number? ','s');
z = y + num;
if z >= 13
disp(' Too High! ')
elseif z <= 11
disp(' Too Low!')
else; z = x;
disp(' Correct! ')
break
end
end

1 Kommentar

Walter Roberson
Walter Roberson am 30 Okt. 2020
What is it that is to be displayed at the end? The sequence of Too High / Too low messages for each of the 5 random numbers?
What should be output in the case where the person does not make a correct guess within 5 tries?
Is there a connection between the "5" being the number of random numbers to work with, and the "5" being the number of guesses that the user is permitted ?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Adam Danz
Adam Danz am 30 Okt. 2020

0 Stimmen

The use of input() to collect data from a user is highly unconstrained and gives you, the programmer, very little control over the user's input (see problems with using input()).
There are several input validation functions you can and should use to force the user to enter valid responses. I recommend validateattributes if you want to return an error because it's intuitive and has been around for a while so it will work on later releases of Matlab.
If you want to re-prompt the user to enter a value after the user enters an invalid value, you could use a while loop along with some attribute-checks. For example, to force the user to enter a scalar, positive integer,
for index = 1:5
y = [0,0];
while numel(y)~=1 || mod(y,1)~=0 || y<0
y = input('Guess the number (scaler, positive integer)? ','s')
end
% [INSERT OTHER STUFF....]
end
> "How would i form an array"
As WR mentioned, it depends on what you're storing. Since you're using the 's' flag to return strings, you could store the outputs in a string array or cell array.
An example of cell-array storage,
nLoops = 5;
y = cell(1,nLoops);
for index = 1:nLoops
y{index} = [0,0];
while numel(y{index})~=1 || mod(y{index},1)~=0 || y{index}<0
y{index} = input('Guess the number (scaler, positive integer)? ','s');
end
% [INSERT OTHER STUFF....]
end

3 Kommentare

I don't really understand the code i am just learning.
This is the question
Make a copy of your answer to the previous problem and store all the individual guesses the user makes in an array.
this is the previous code i made
clc,clear
x = 12;
num = 0;
for index = 1:5
y = input('Guess the number? ');
z = y + num;
if z >= 13
disp(' Too High! ')
elseif z <= 11
disp(' Too Low!')
else; z = x;
disp(' Correct! ')
break
end
end
x = 12;
guesses = [];
for index = 1:5
y = input('Guess the number? ');
guesses(index) = y;
if y > x
disp(' Too High! ')
elseif y < x
disp(' Too Low!')
else
disp(' Correct! ')
break
end
end
Kalpha.mc
Kalpha.mc am 31 Okt. 2020
Thank You!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020a

Tags

Gefragt:

am 30 Okt. 2020

Kommentiert:

am 31 Okt. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by