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

2 ビュー (過去 30 日間)
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.

採用された回答

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by