How to create a loop?

1 次查看(过去 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 中查找有关 Code Generation for ARM Cortex-M and ARM Cortex-A Processors 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by