Getting a script to continue to ask for a user specified input value until a certain input is entered.
이전 댓글 표시
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!