While loop with if function

11 visualizaciones (últimos 30 días)
Ken L
Ken L el 30 de En. de 2021
Comentada: Ken L el 2 de Feb. de 2021
Hi, need some help on whie loop and if function.
E.g,
Amount =$1-500
type = a or b
How should I write in mathlab, if I wan if the amount between 1-500 and if the type is "a" then show the 'a" result else showing "b" result. And after that will ask whether user want to continue the process or not.

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de En. de 2021
if 1 <= amount & amount <= 500 & strcmp(type, 'a')
  9 comentarios
Walter Roberson
Walter Roberson el 1 de Feb. de 2021
while true
name = input("Enter name:","s");
address = input("Enter address:","s");
purchase_amount = input("Enter amount of purchase:");
purchase_type = input("Enter type of purchase (L for type L/ D for type D):","s");
fail = false;
if strcmpi(purchase_type, "D") && purchase_amount >= 0 && purchase_amount <= 250
discount = 0.95;
net_amount = purchase_amount*discount;
elseif strcmpi(purchase_type, "D") && purchase_amount >= 251 && purchase_amount <= 570
discount = 0.924;
net_amount = purchase_amount*discount;
elseif strcmpi(purchase_type, "D") || strcmpi(purchase_type, "L")&& purchase_amount >= 0 && purchase_amount <= 250
discount = 1;
net_amount = purchase_amount*discount;
elseif strcmpi(purchase_type, "D") || strcmpi(purchase_type, "L")&& purchase_amount >= 251 && purchase_amount <= 570
discount = 0.95;
net_amount = purchase_amount*discount;
else
fprintf ("Invalid type.\n")
fail = true;
end
if ~fail
fprintf("Name: %s\n",name);
fprintf("Address: %s\n",address);
fprintf("Net Amount: $%d\n",net_amount);
end
prompt = input("Do you wish to continue to purchase (Y for Yes/ N for No):","s");
if ~strcmpi(prompt, 'Y')
break;
end
end
fprintf("Goodbye.\n");
Ken L
Ken L el 2 de Feb. de 2021
Hi Walter, thank you very much, the code worked perfectly, really appreciate your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by