How to create a loop?

2 visualizaciones (últimos 30 días)
John Carlo
John Carlo el 12 de En. de 2023
Editada: VBBV el 12 de En. de 2023
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

Respuestas (1)

VBBV
VBBV el 12 de En. de 2023
Editada: VBBV el 12 de En. de 2023
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 comentarios
VBBV
VBBV el 12 de En. de 2023
Editada: VBBV el 12 de En. de 2023
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 el 12 de En. de 2023
There are several ways in which you can re-run the code, e.g shown above is another method

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by