How am I gonna do if I just want this to show me only Decoy message: Invalid rescue day . And the changes will not affect other codes?

clc;clear
C1 = input('Please enter a code to break: ' , 's');
if length(C1) ~= 6
disp('Decoy message: Not a six-digit number.');
else
A = mod(sum(C1 - '0'),2);
if A == 1
disp('Decoy message: Sum is odd.')
else
C2 = (C1(1) - '0') * (C1(2) - '0') - (C1(3) - '0');
switch C2
case 1
Day = 'Rescue on Monday';
case 2
Day = 'Rescue on Tuesday';
case 3
Day = 'Rescue on Wednesday';
case 4
Day = 'Rescue on Thursday';
case 5
Day = 'Rescue on Friday';
case 6
Day = 'Rescue on Saturday';
case 7
Day = 'Rescue on Sunday';
otherwise
disp('Decoy message: Invalid rescue day.');
end
F1 = str2double(C1(4));
F2 = str2double(C1(5));
F3 = str2double(C1(6));
if mod(F1,3) == 0
B = F2 - F3;
else
B = F3 - F2;
end
switch(B)
case 1
Position = ' at the bridge.';
case 2
Position = ' at the library.';
case 3
Position = ' at the river crossing.';
case 4
Position = ' at the airport.';
case 5
Position = ' at the bus terminal.';
case 6
Position = ' at the hospital.';
case 7
Position = ' at St. Petes Church.';
otherwise
disp('Decoy message: Invalid rendezvous point.');
end
disp([Day , Position])
end
end
When I enter 397090, I will get both:
Decoy message: Invalid rescue day.
Decoy message: Invalid rendezvous point.
and some other error messages:
Unrecognized function or variable 'Day'.
Error in Untitled5 (line 69)
disp([Day , Position])
How am I gonna do if I just want this to show me only Decoy message: Invalid rescue day . And the changes will not affect other codes?

 Risposta accettata

Before
switch C2
insert
failed = false;
After
disp('Decoy message: Invalid rescue day.');
insert
failed = true;
Put the
switch(B)
inside
if ~failed

9 Commenti

Yes, together with all the appropriate case statements that belong to switch(B); they all go before the "end"
Thank you. And I got another question
if I enter :918990
it will give me decoy message: Invalid rendezvous point.
But also there is a error massage showed up:
Unrecognized function or variable 'Position'.
Error in Untitled5 (line 72)
disp([Day , Position])
Can I avoid this?
Make sure the disp() is inside your "if ~failed"
Sorry, like where?
clc;clear
C1 = input('Please enter a code to break: ' , 's');
if length(C1) ~= 6
disp('Decoy message: Not a six-digit number.');
else
A = mod(sum(C1 - '0'),2);
if A == 1
disp('Decoy message: Sum is odd.')
else
C2 = (C1(1) - '0') * (C1(2) - '0') - (C1(3) - '0');
failed = false;
switch C2
case 1
Day = 'Rescue on Monday';
case 2
Day = 'Rescue on Tuesday';
case 3
Day = 'Rescue on Wednesday';
case 4
Day = 'Rescue on Thursday';
case 5
Day = 'Rescue on Friday';
case 6
Day = 'Rescue on Saturday';
case 7
Day = 'Rescue on Sunday';
otherwise
disp('Decoy message: Invalid rescue day.');
failed = true;
end
F1 = str2double(C1(4));
F2 = str2double(C1(5));
F3 = str2double(C1(6));
if mod(F1,3) == 0
B = F2 - F3;
else
B = F3 - F2;
end
if ~failed
switch(B)
case 1
Position = ' at the bridge.';
case 2
Position = ' at the library.';
case 3
Position = ' at the river crossing.';
case 4
Position = ' at the airport.';
case 5
Position = ' at the bus terminal.';
case 6
Position = ' at the hospital.';
case 7
Position = ' at St. Petes Church.';
otherwise
disp('Decoy message: Invalid rendezvous point.');
end
disp([Day , Position])
end
end
end
I don't quite get it, if you put disp([Day , Position]) in switch(B),
right below disp('Decoy message: Invalid rendezvous point.'); This will affect the original code...
Well, instead you could change the
disp('Decoy message: Invalid rescue day.');
to
Day = 'Decoy message: Invalid rescue day.';
and change
disp('Decoy message: Invalid rendezvous point.');
to
Position = 'Decoy message: Invalid rendezvous point.';
I tired
otherwise
disp('Decoy message: Invalid rendezvous point.');
disp([Day , Position])
end
or
otherwise
Position: 'Decoy message: Invalid rendezvous point.';
disp([Day , Position])
end
or with the
disp([Day , Position]) after end
It just does not work, what I want is if I enter 918990, I only want it shows me
Decoy message: Invalid rendezvous point.
with no error messages showed up.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by