If statement for input value

조회 수: 1 (최근 30일)
Jarren Berdal
Jarren Berdal 2020년 8월 5일
답변: Star Strider 2020년 8월 5일
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
madhan ravi
madhan ravi 2020년 8월 5일
And what’s wrong with the above code?

댓글을 달려면 로그인하십시오.

채택된 답변

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