If statement for input value

1 次查看(过去 30 天)
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..

采纳的回答

Star Strider
Star Strider 2020-8-5
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.

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by