While loop with if function

11 ビュー (過去 30 日間)
Ken L
Ken L 2021 年 1 月 30 日
コメント済み: Ken L 2021 年 2 月 2 日
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.

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 30 日
if 1 <= amount & amount <= 500 & strcmp(type, 'a')
  9 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 1 日
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 2021 年 2 月 2 日
Hi Walter, thank you very much, the code worked perfectly, really appreciate your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by