How to create a loop?

2 visualizzazioni (ultimi 30 giorni)
John Carlo
John Carlo il 12 Gen 2023
Modificato: VBBV il 12 Gen 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

Risposte (1)

VBBV
VBBV il 12 Gen 2023
Modificato: VBBV il 12 Gen 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 Commenti
VBBV
VBBV il 12 Gen 2023
Modificato: VBBV il 12 Gen 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 il 12 Gen 2023
There are several ways in which you can re-run the code, e.g shown above is another method

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by