My code works except variable s remains fixed to 20 when it should be changing depending on the values of input for variable x

1 次查看(过去 30 天)
Can someone help me with my code? I'm trying to calculate the cost (x), taxes (t), and shipping cost (s) but for some reason the shipping doesn't change with the parameters I put.
prompt='How many giant slinkys do u wanna buy? ';
x=input(prompt);
if x<=10;
x= 21.99*x;
elseif (11>=x)&(x<=50);
x=x*19.99;
elseif (51>=x)&(x<=100);
x= x*17.99;
else x >= 100;
x= x*15;
end
if x<=10;
s=10;
elseif (x>10)&(x<=20);
s=20;
elseif (x>20)&(x<=30);
s=30;
elseif (x>30)&(x<=40);
s=40;
elseif (x>40)&(x<=49);
s=50;
elseif (x>=50);
s=0;
end
disp('Cost will be ')
disp(x);
t=x+.095*x;
disp('sales tax')
disp(t);
disp('Shipping cost')
disp(s);
T=x+t+s;
disp('The total cost is')
disp(T)
  2 个评论
Stephen23
Stephen23 2018-3-2
Oscar Muneton's "Answer" moved here:
Make sure the user's input is a positive integer.
10 or fewer Slinkys cost $21.99 each.
11 to 50 Slinkys are $19.99 each
51 to 100 Slinkys are $17.99 each
More than 100 Slinkys cost $15 each.
Sales tax is 9.5%
Shipping is $10 for each group of 10 Slinkys. For example: ◦ Shipping for 1 Slinky is $10; Shipping for 8 is also $0; Shipping for 10: $10. ◦ Shipping for 11 Slinkys is $20; Shipping for 15, also $20; Shipping for 20: $20. • Shipping is free for orders of 50 or more Slinkys
this is the homework problem idk if im doing something wrong with my code
Stephen23
Stephen23 2018-3-2
David Fletcher's "Answer" moved here:
In the second set of decision statements you are not still not testing the original number that was input by the user, instead you are testing that number multiplied by the scaler. I don't know if this is what you want to do or not, but I suspect it isn't. If indeed you want to test the original input you can replace the c with the x in the second decision block (since you have now preserved the original input of x by assigning the calculation in the first decision block to a new variable).

请先登录,再进行评论。

采纳的回答

David Fletcher
David Fletcher 2018-3-1
The value of x will have been changed by your first decision structure, so by the time you test it again in the second decision tree it will be much higher than it was originally since you have already scaled it up by a factor. I suspect that this isn't intentional and instead of changing x you want to reassign x to a new variable.
  2 个评论
Oskore Mune
Oskore Mune 2018-3-1
prompt='How many giant slinkys do u wanna buy? ';
x=input(prompt);
if x<=10;
c= 21.99*x;
elseif (11>=x)&&(x<=50);
c=x*19.99;
elseif (51>=x)&&(x<=100);
c= x*17.99;
else x >= 100;
c= x*15;
end
if c<=10;
s=10;
elseif (c>10)&&(c<=20);
s=20;
elseif (c>20)&&(c<=30);
s=30;
elseif (c>30)&&(c<=40);
s=40;
elseif (c>40)&&(c<=49);
s=50;
elseif (c>=50);
s=0;
end
disp('Cost will be ')
disp(c);
t=c+.095*c;
disp('sales tax')
disp(t);
disp('Shipping cost')
disp(s);
T=c+t+s;
disp('The total cost is')
disp(T)
okay I changed the cost variable from x to c but im still not getting my shipping to show up at the end. It stays 0

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by