If statement for input value

1 vue (au cours des 30 derniers jours)
Jarren Berdal
Jarren Berdal le 5 Août 2020
I want the user to give a number, depending on that number gives the user a number of questions to answer
ex
n = input('how many ingredients do you have');
%%FOOD INPUT
if n =1
a = input('Type code of Ingrediant #1: ');
A = input('Type quantity of Ingrediant #1: ');
end
if n =2
a = input('Type code of Ingrediant #1: ');
A = input('Type quantity of Ingrediant #1: ');
b= input('Type code of Ingrediant #2: ');
B= input('Type quantity of Ingrediant #2: ');
end
etc..
  1 commentaire
madhan ravi
madhan ravi le 5 Août 2020
And what’s wrong with the above code?

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 5 Août 2020
I would just use a loop:
n = input('how many ingredients do you have');
%%FOOD INPUT
for k = 1:n
a(k) = input(sprintf('Type code of Ingrediant #%d: ',k));
A(k) = input(sprintf('Type quantity of Ingrediant #%d: ',k));
end
Then do whatever you want with the resulting vectors.

Plus de réponses (0)

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by