Getting a script to continue to ask for a user specified input value until a certain input is entered.

3 vues (au cours des 30 derniers jours)
Hello, I first just want to say how helpful this site is. Anyway, I am trying to create a script where as soon as the script is run, it automatically goes back to the start and asks a user for a new input, and will terminate when the user enters 0 as the input value. Script is shown below for reference:
clear;clc;
x = input('Enter kilowatt-hour used, (Enter 0 to terminate): ');
if x <= 300
cost = 0.09*x;
elseif x <= 600
cost = 300*0.09 + 0.08*(x-300);
elseif x <= 1000
cost = 300*0.09 + 0.08*300 + 0.06*(x-600);
else
cost = 300*0.09 + 0.08*300 + 0.06*400 + 0.05*(x- 1000);
end
disp(['The total cost is ', num2str(cost), ' dollars'])
I have all the calculations done, I just need a bit of guidance for getting it to continually ask for an x=input (shown on second line) when the script file is run , and finally terminate when the user enters 0 when prompted for an input. Is there a relatively simple way of doing this?
Any help at all is greatly appreciated, Thanks.

Réponse acceptée

Geoff
Geoff le 16 Mar 2012
Here you go =)
while 1
x = input('etc etc');
if x == 0
break;
elseif x <= 300
% .
% .
% .
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by