Help with If else statements
Show older comments
I wrote a script that reads data from the internet and predicts the winner of a football game. The model I use assigns a total of 10 points to each team, so the closest win is 6-4 or a 5-5 draw. I display the winner of the game in the GUI with a text box with the code,
if true
% code
if TeamAScore > TeamBScore
set(handles.text4,'String', TeamAName);
else
set(handles.text4,'String', TeamBName);
end;
end
This works great, but I wanted to spruce it up by letting the user know how big the win is. So I wrote,
if true
%Display winner
WinBig = ' wins big.';
WinComfort = ' wins comfortably';
WinClose = ' wins close';
if TeamAScore >= 9
disp(TeamAName); %win big
NewName = [TeamAName, WinBig];
set(handles.text4,'String', NewName);
elseif TeamAScore == 8 || 7
disp(TeamAName); % comfortable win
NewName = [TeamAName, WinComfort];
set(handles.text4,'String', NewName);
elseif TeamAScore == 6
disp(TeamAName); % close win
NewName = [TeamAName, WinClose];
set(handles.text4,'String', NewName);
elseif TeamBScore >=9
disp(TeamBName); % win big
NewNameB = [TeamBName, WinBig];
set(handles.text4,'String', NewNameB);
elseif TeamBScore == 8 || 7
disp(TeamBName); % comfortable win
NewNameB = [TeamBName, WinComfort];
set(handles.text4,'String', NewNameB);
elseif TeamBScore == 6
disp(TeamBName); % close win
NewNameB = [TeamBName, WinClose];
set(handles.text4,'String', NewNameB);
elseif TeamBScore == TeamAScore
disp('Too close to call'); % too close to call
set(handles.text4,'String', 'Too close to call');
end;
% code
end
But this gives me TeamA everytime, not sure why. Any help is appreciated.
Accepted Answer
More Answers (1)
Image Analyst
on 5 Dec 2015
This is incorrect syntax:
elseif TeamAScore == 8 || 7
You'd need this:
elseif TeamAScore == 8 || TeamAScore== 7
or, better and simpler:
elseif TeamAScore >= 7
Same problem for TeamBScore a little further down.
Categories
Find more on Board games in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!