1

I've made a simple volume of a sphere calculator w/ user input:

clc

%Ask For Radius
radius = input ('What is the radius of your sphere?');

%Calculate Volume Using Radius
vlm = (4/3) * pi * radius^3 ;

%Return Radius
fprintf('The volume of your sphere is %4.2f inches cubed\n', vlm)

But I decided I wanted to go one step further and give the user the option to ask another and another until they don't want to do anymore. However I'm stuck as I can't figure out how to take this user input:

again = input ('Do You Want To Find The Volume Of A Sphere?','s')

And apply it to a conditional such as and if else statement. Here is what I've come up with so far:

if again = yes
    %Ask For Radius
    %Insert VolumeOfSphere.m
else 
    fprintf('Ok, Just run Me Again If You Want To Calculate Another Sphere')
    %Add An End program Command
end

Any help or tips in the right direction is very much appreciated in advance.

1
  • while Commented Jan 19, 2018 at 17:48

1 Answer 1

1

excaza is right, a while loop sounds like what you need. A basic implementation of this might be:

clc; clear;

while true
    %Ask For Radius
    radius = input ('What is the radius of your sphere?  ');

    %Calculate Volume Using Radius
    vlm = (4/3) * pi * radius^3 ;

    %Return Radius
    fprintf('The volume of your sphere is %4.2f inches cubed\n', vlm)

    % Ask if they want to run again
    run_again = input('\nWould you like to run again? Y or N:  ', 's');

    % Deal with their answer
    if     strcmpi(run_again, 'Y')
        % do nothing, let the loop keep running
    elseif strcmpi(run_again, 'N')
        break % this will break out of the while loop and end the program
    else
        % assume they meant Y, and send them back to the start
    end

end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.