How to create a loop?

조회 수: 3 (최근 30일)
John Carlo
John Carlo 2023년 1월 12일
편집: VBBV 2023년 1월 12일
How can I loop this code such that after getting the answer the code should re run by itself?
clear all
clc
format longG;
format compact;
txt = input ('Choose a Production Period: 1) Annual 2) Monthly 3) Weekly 4) Daily');
if txt == 1
prompt = "Input Annual Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/10/4/7/2
elseif txt == 2
prompt = "Input Monthly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/4/7/2
elseif txt == 3
prompt = "Input Weekly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/7/2
elseif txt == 4
prompt = "Input Daily Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/2
else
disp("INVALID")
end

답변 (1개)

VBBV
VBBV 2023년 1월 12일
편집: VBBV 2023년 1월 12일
clear all
clc
format longG;
format compact;
txt = input ('Choose a Production Period: 1) Annual 2) Monthly 3) Weekly 4) Daily');
if txt == 1
prompt = "Input Annual Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/10/4/7/2
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
end
elseif txt == 2
prompt = "Input Monthly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/4/7/2
% similarly for this input
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor
end
elseif txt == 3
prompt = "Input Weekly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/7/2
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor
end
elseif txt == 4
prompt = "Input Daily Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/2
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor
end
else
disp("INVALID")
end
  댓글 수: 3
VBBV
VBBV 2023년 1월 12일
편집: VBBV 2023년 1월 12일
while 1 % this is another way
txt = input ('Choose a Production Period: 1) Annual 2) Monthly 3) Weekly 4) Daily');
if txt == 1
prompt = "Input Annual Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/10/4/7/2
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
elseif txt == 2
prompt = "Input Monthly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/4/7/2
% similarly for this input
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
elseif txt == 3
prompt = "Input Weekly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/7/2
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
elseif txt == 4
prompt = "Input Daily Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/2
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
else
disp("INVALID")
end
end %
VBBV
VBBV 2023년 1월 12일
There are several ways in which you can re-run the code, e.g shown above is another method

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by