I'm trying to create a script that will allow the user to input x-values repeatedly until each case has been entered. I have three possible cases:
- Case 1 is entered when
x <= 7 - Case 2 is entered when
7 <= x <= 12. - Case 3 is entered when
x > 12
I want to use a while statement to error-check the user input, ensuring that x > 0. Each time a case is entered, I want to print the case number & the created y-value:
y = x^3 + 3for case 1y = (x-3)/2for case 2y = 4*x+3for case 3
No case may be ran twice. The script will output something like 'That case has been run already' should this happen. Once all cases have been entered, I want to print something like 'All cases have been entered'.
Here is what I have tried so far:
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!')
The only thing I can't seem to get to work now is this part:
if counter1>=1 || counter2>=1 || counter3>=1
disp('That case has been run already');
It seems to be ignored entirely. Any suggestions?