我的MATLAB代码有什么问题? (初学者在这里)

时间:2014-10-29 22:15:58

标签: matlab

功能1:

  function [x,y]=func1(x,y)
      x = input('Enter how many tickets you would like: ');
      y = input('Enter what level you would like to sit in: ');
  end        

功能2:

  function [z] = func2(x,y)
  switch l
      case y==1
       z = 500*x;


  case y==2
   z = 350*x;

  case y==3
   z = 200*x;


end

功能3:

  function [x,y,z]=func3(x,y,z)
  [x,y]=func1(x,y);
  z=func2(x,y);
  fprintf('It will cost $%.2f for %d tickets in level %d.','z','x','y');
  end

我想连续调用这三个函数(这就是我尝试的方法):

  func1(x,y)
  func2(x,y)
  func3(x,y,z)

我想只打印三行(除了我希望打印实际计算而不是x,y,z):

  Enter how many tickets you would like:
  Enter what level you would like to sit in:
  It will cost $z for x tickets in level y. 

这两行中的第一行很好,但第三行打印随机数代替x,y,z。我做错了什么?

1 个答案:

答案 0 :(得分:3)

您的fprintf声明应为

fprintf('It will cost $%.2f for %d tickets in level %d.',z,x,y);

您希望存储在x, y, z中的值,而不是字符串'x', 'y', 'z'