hello Dear!! I'm a new coming in the community I want your help. about coding on MATLAB does the code is correct ? or the using of (&&) is no faire? and how can I correct it? thank you for respond .
조회 수: 29 (최근 30일)
이전 댓글 표시
if app.LikeCheckBox.Value==true
app.EditField.Value='you Like our channal'
elseif app.LikeCheckBox.Value==true && app.ShareCheckBox.Value==true
app.EditField.Value='you liked and shared'
elseif app.ShareCheckBox.Value==true
app.EditField.Value='you Share our channal'
elseif app.ShareCheckBox.Value==true && app.SubscribeCheckBox.Value==true
app.EditField.Value='you shared and subscribed'
elseif app.SubscribeCheckBox.Value==true
app.EditField.Value='you Subscribed to our channal'
elseif app.LikeCheckBox.Value==true && app.SubscribeCheckBox.Value==true
app.EditField.Value='good job'
else app.EditField.Value='Choose on item'
app.EditField.HorizontalAlignment='center'
end
댓글 수: 0
답변 (3개)
Jacob Mathew
2024년 11월 21일 16:09
Hey god,
You can use double ampersand (&&) as logical and with an if statement. The following piece of code demonstrates the same:
a = true;
b = true;
c = false;
a && b
a && c
댓글 수: 0
Image Analyst
2024년 11월 21일 16:16
The double && is correct. However you don't need to do a logical comparison to true. If it's true, then app.LikeCheckBox.Value==true will return true when it evaluates that equality expression, which is just the same as app.LikeCheckBox.Value. So you can more simply just do
elseif app.LikeCheckBox.Value && app.ShareCheckBox.Value
etc. and not use the ==true.
댓글 수: 0
Walter Roberson
2024년 11월 21일 19:37
if app.LikeCheckBox.Value==true
app.EditField.Value='you Like our channal'
elseif app.LikeCheckBox.Value==true && app.ShareCheckBox.Value==true
Consider that if LikeCheckBox value is true then the first action will be triggered -- and no further checking of elseif will be done. So the only way to trigger the elseif is if LikeCheckbox is something other than true. But if LikeCheckBox value is something other than true, then it cannot be true for the purpose of the app.LikeCheckBox.Value==true test in the elseif clause.
You have two major choices:
- You can check the most exclusive set of conditions first -- liked and shared and subscribed -- and then elseif different subsets to narrow down the possibilities.
- You can build up a set of flags, flags(1,3) = false; flags(1) = app.LikeCheckBox.Value; flags(2) = app.ShareCheckBox.Value; flags(3) = app.SubscribeCheckBox.Value; . Now build up strings based on the flags. Such as if flags(2); if any(flags(1)) State = State + " and "; end; State = State + "shared"; end . At the end, the complete string will have been built up.
댓글 수: 1
Steven Lord
2024년 11월 21일 20:01
Or rather than just using if / elseif / elseif / else, you could include some nested if statements to give some structure to the checking.
if app.LikeCheckBox.Value
% You at least liked the channel, now let's see about sharing /
% subscribing
if app.ShareCheckBox.Value
% You liked and shared, so let's check subscribed
if app.SubscribeCheckBox.Value
str = "You Liked, Shared, and Subscribed to our channel";
else
str = "You Liked and Shared our channel";
end
else
% You liked but didn't share, check subscribed
if app.SubscribeCheckBox.Value
str = "You Liked and Subscribed to our channel";
else
str = "You Liked our channel";
end
end
else
% You didn't like the channel
if app.ShareCheckBox.Value
if app.SubscribeCheckBox.Value
str = "You Shared and Subscribed to our channel";
else
str = "You Shared our channel";
end
else
if app.SubscribeCheckBox.Value
str = "You Subscribed to our channel";
else
str = "Why didn't you Like, Share, or Subscribe to the channel?";
end
end
end
From here you can see that all eight combinations of options are covered, by inspection of the eight messages assigned to the variable str. This would get unwieldy if you had more options or if you had more than two possible values for the options, but 2^3 options IMO is still manageable.
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!