terminating a script with a quit comand

3 次查看(过去 30 天)
I have been told to make a script that will evaluate a string, and at the end it must ask the user if they are sure they want to quit if yes then quit if no then rerun the switch.
this is an example of what i need it to look like for choice 7:
Please choose an option:
1. Length
2. First letter
3. Last letter
4. Reverse
5. All
6. Enter a new string
7. Quit
Choice: 7
Are you sure you would like to quit (Yes/No)? n
Please choose an option:
1. Length
2. First letter
3. Last letter
4. Reverse
5. All
6. Enter a new string
7. Quit
Choice: 7
Are you sure you would like to quit (Yes/No)? y
Quitting program
>>
my code looks like:
clear; clc; close all;
choice = input('please give a "string"(PHRASE) to be evaluated: ', 's');
%this is a function that i created that evaluates the string.
[length firstLetter lastLetter reverse all] = singletary6(choice);
%function [stringLength, firstLetter, lastLetter, reverse, all] = singletary6(str)
%if isempty(str)
% stringLength = 0;
% firstLetter = '';
% lastLetter = '';
% reverse = '';
% all = '';
%else ~isempty(str);
% stringLength = length(str);
% firstLetter = str(1);
% lastLetter = str(length(str));
% reverse = str(end:-1:1);
% all = str;
%end
%end
disp(choice)
menu = sprintf('1. length\n2. First letter\n3. Last Letter\n4. Reverse\n5. All\n6. Enter a new String\n7. Quit\n');
fprintf('1. length\n2. First letter\n3. Last Letter\n4. Reverse\n5. All\n6. Enter a new String\n7. Quit\n');
choose = input('Choose a comand from 1-7:');
while choose <= 7
switch choose
case 1
length
disp(menu)
choose = input('Choose a comand from 1-7:');
case 2
firstLetter
disp(menu)
choose = input('Choose a comand from 1-7:');
case 3
lastLetter
disp(menu)
choose = input('Choose a comand from 1-7:');
case 4
reverse
disp(menu)
choose = input('Choose a comand from 1-7:');
case 5
all
disp(menu)
choose = input('Choose a comand from 1-7:');
case 6
choice = input('Enter a new String(phrase)', 's');
[length firstLetter lastLetter reverse all] = singletary6(choice);
fprintf('\n')
disp(menu)
choose = input('Choose a comand from 1-7:');
fprintf('\n')
case 7
disp('Quit')
button = sprintf('Ready to quit?\nYes\nNo\n');
switch button
case 'Yes',
disp('Exiting MATLAB');
%Save variables to matlab.mat
save
case 'No',
quit cancel;
end
end
end
%i know there is a lot of stuff here but wanted to give as much as possible to help with an answer.
  1 个评论
SonOfAFather
SonOfAFather 2012-10-4
while i have continued to work this i have found that case 7 which now looks like this:
case 7
disp('Quit')
terminate = input('Are you sure you want to quit(yes/no)', 's');
switch terminate
case 'no'
disp(menu)
choose = input('Choose a comand from 1-7:');
case 'yes'
terminate = 0;
end
no works, but yes is greating a restatement of case 7. any ideas?

请先登录,再进行评论。

采纳的回答

Matt Fig
Matt Fig 2012-10-4
编辑:Matt Fig 2012-10-4
Since the SWITCH is in the loop, why not just set choose to 10 if the user wants to quit? Then the loop condition will fail and the program will cease...
case 7
disp('Quit??')
terminate = input('Are you sure?(yes/no)', 's');
if strcmp(terminate,'no')
disp(menu)
choose = input('Choose a comand from 1-7:');
else
choose = 10; % Now the WHILE condition will fail.
disp('bye-bye!')
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by