创建一个脚本,允许用户重复输入值,直到输入每个案例

时间:2014-11-19 21:14:42

标签: matlab

我试图创建一个脚本,允许用户重复输入x值,直到输入每个案例为止。我有三种可能的情况:

  • x <= 7
  • 时输入案例1
  • 7 <= x <= 12时输入案例2。
  • x > 12
  • 时输入案例3

我想使用while语句对用户输入进行错误检查,确保x > 0。每次输入案例时,我都要打印案例编号&amp;创建的y值:

    案例1
  • y = x^3 + 3
  • 案例2的
  • y = (x-3)/2
  • 案例3的
  • y = 4*x+3

任何情况都不得跑两次。该脚本将输出类似于&#39;该案例已经运行的内容&#39;如果发生这种情况输入所有案例后,我想要打印类似于&#39;所有案例都已输入&#39;。

这是我到目前为止所尝试的内容:

  counter1 = 0;
  counter2 = 0;
  counter3 = 0;
  while counter1==0 || counter2==0 || counter3==0
  x = input('Please enter an x value > 0: ');
  while x < 0
      x = input('Invalid! Please enter another x value > 0: ');
  end
  if counter1>=1 || counter2>=1 || counter3>=1
      disp('That case has been run already');
  elseif x<=7
      counter1 = counter1 + 1;
      y = x.^3 + 3;
      fprintf('Case 1: y = %d \n',y);
  elseif 7<x && x<=12
      counter2 = counter2 + 1;
      y = (x-3)./2;
      fprintf('Case 2: y = %d \n',y);
  elseif x>12
      counter3 = counter3 + 1;
      y = 4.*x+3; 
      fprintf('Case 3: y = %d \n',y);
  else counter1==1 && counter2==1 && counter3==1;
  end
  end
  disp('All cases have been entered!')

我现在唯一能够开始工作的是这部分:

  if counter1>=1 || counter2>=1 || counter3>=1
      disp('That case has been run already');

似乎完全被忽略了。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

取消所有counter个变量。改为使用boolean / logical标志来指示特定案例的执行时间。此外,您还需要检查是否已在每个案例本身内执行了特定案例。不要将此作为单独的外部if语句执行。这可能就是为什么你最初写的那句话不起作用的原因。因此,做这样的事情。我将%//NEW放在新代码中添加的地方:

  case1 = false; %// NEW
  case2 = false; %// NEW
  case3 = false; %// NEW
  while ~case1 || ~case2 || ~case3 %// NEW: While at least one of the cases has not been run...
      x = input('Please enter an x value > 0: ');
      while x < 0
          x = input('Invalid! Please enter another x value > 0: ');
      end

      if x <= 7 %// NEW
         if case1 %// NEW: Check if case #1 has already been run
             %// If it has, show this to the user, then continue in the loop
             fprintf('Case #1 has already been run!\n');
             continue; %// NEW - Continue through the loop.  Don't do anything else
         end

         case1 = true; %// NEW - Set to true if we haven't run this case already
         y = x.^3 + 3;
         fprintf('Case 1: y = %d \n',y);

      elseif 7<x && x<=12
         if case2 %// NEW - Repeat like Case #1 here
            fprintf('Case #2 has already been run!\n'); %// NEW
            continue; %// NEW
         end

         case2 = true; %// NEW
         y = (x-3)./2;
         fprintf('Case 2: y = %d \n',y);

      elseif x>12
         if case3 %// NEW - Repeat like Case #3 here
             fprintf('Case #3 has already been run!\n'); %// NEW
             continue; %// NEW
         end

         case3 = true; %// NEW
         y = 4.*x+3; 
         fprintf('Case 3: y = %d \n',y);
       end %// End if
   end %// End while
   disp('All cases have been entered!') %// Display once all cases have been entered

这是一个示例运行,显示它有效:

Please enter an x value > 0: -1
Invalid! Please enter another x value > 0: 3
Case 1: y = 30 
Please enter an x value > 0: 2
Case #1 has already been run!
Please enter an x value > 0: 8
Case 2: y = 2.500000e+00 
Please enter an x value > 0: 10
Case #2 has already been run!
Please enter an x value > 0: 14
Case 3: y = 59 
All cases have been entered!

我输入-1试试看它是否拒绝负数,这是做的。我输入了一个< 7的数字,即3,它成功地输入了第一个案例。我尝试输入另一个< 7的数字,即2,它给出的消息说已经运行了#1。我尝试输入一个介于7到12之间的数字...所以我尝试8.它会相应地生成Case#2。我再试一次10,它说Case#2已经运行了。最后,我尝试14,即> 12,它生成案例#3并在所有案例成功运行时停止。

我相信这就是你要找的东西。