Can this simple script be done without calling a function?

1 回表示 (過去 30 日間)
Austin
Austin 2013 年 9 月 24 日
hi,im new to matlab here
im trying to write a script that would prompt a user if he have only $6 in his pocket,whether he would like to buy apples or oranges(mutually exclusive,he cannot choose to buy apple if he bought orange vice versa) and lastly show him how many apple or orange he can buy depending on which option he chose.(the cost of apple is $0.50 each while cost of orange is $1.50 each.)
can i do these in a single script without calling any functions?
this is what i tried:
AorO=input('would you like to buy apples or oranges?')
if AorO==A
A=6/.5
fprintf('the amount of orange you can buy is %.2f\n',O)
else AorO==O
O=6/1.5
fprintf('the amount of orange you can buy is %.2f\n',O)
however i keep getting this error: Undefined function or variable A or O...
can the experts here kindly advise on my script?
Thanks lot in advance!!

回答 (3 件)

Walter Roberson
Walter Roberson 2013 年 9 月 24 日
AorO=input('would you like to buy apples or oranges?', 's')
if AorO=='A'
and so on
  1 件のコメント
Austin
Austin 2013 年 9 月 24 日
i see the only difference from you answer an my script is adding '' to A and ,'s' to my input.
i have done that but ends up with the same error popping up: undefined function or variable 'A' or undefined function or variable 'O'

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


Austin
Austin 2013 年 9 月 24 日
AorO=input('would you like to buy apples or oranges?','s')
if AorO=='A' A=6/.5
fprintf('the amount of orange you can buy is %.2f\n',O)
else AorO=='O'
O=6/1.5
fprintf('the amount of orange you can buy is %.2f\n',O)
this keeps popping out-->undefined function or variable 'A'
or must i do it via "calling a function way'?:(

Ilham Hardy
Ilham Hardy 2013 年 9 月 24 日
Please learn how to use debug mode..
AorO=input('would you like to buy apples or oranges?','s');
if AorO=='A'
A=6/.5;
fprintf('the amount of orange you can buy is %.2f\n',A)
elseif AorO=='O';
O=6/1.5;
fprintf('the amount of orange you can buy is %.2f\n',O)
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by