how to make if accept letters?

3 次查看(过去 30 天)
i know about 'if' code, but what i dont understand is why it tells me error?
error: 'y' undefined near line 33 column 16
error: called from
Code_6 at line 33 column 5
nam = input('Enter name: ', 's');
adrs = input('Enter address: ', 's');
num = input('Enter amount of purchase: $');
pur = input('Enter type of purchase (L for Laptop / D for Desktop): ', 's');
fprintf('\n')
switch pur
case {'L' 'l'}
disp('You have choosen Laptop as your type of puchase')
if num <250
disc = (0 / 100) * num;
net = num - disc;
elseif num <570
disc = (5 / 100) * num;
net = num - disc;
elseif num <1000
disc = (7.5 / 100) * num;
net = num - disc;
elseif num >1000
disc = (10 / 100) * num;
net = num - disc;
endif
fprintf('Name: %s\n', nam)
fprintf('Address: %s\n', adrs)
fprintf('Net amount: $%.2f \n', net)
#promt user to continue or not
yesno = input('Do you wish to continue purchase? (y/n)', 's')
fprintf('\n')
if yesno = y;
Code_6
else
disp('You have exit purchase screen, Goodbye.')
endif
  1 个评论
per isakson
per isakson 2020-11-28
编辑:per isakson 2020-11-28
Firstly, replace endif by end. And it's a good habit to have an else-clause in every if-end statement.

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-11-28
Several problem:
1. = is an operator for assignment in MATLAB. For comparison, you need to use ==
if yesno == y
2. You want to compare the value to character 'y', just writing y means that you are referring to a variable
if yesno == 'y'
3. It is more of a suggestion, but it us better to use strcmp() for char arrays
if strcmp(yesno,'y')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by