App Designer- How to make button display answers in different numeric fields depending on the clicks

25 次查看(过去 30 天)
Hello, Im currently designing a game in App designer where you can click a button and it displays a random number between 1-10. Currently in my app, when you click the "roll" button it generates 4 different numbers into 4 different text fields and I don't want that. I want the button when clicked once it displays one random generated number in the first text box and when I click roll again it displays another number in the second box and etc... for the next two clicks. Heres an example of my code below:
function RollValueChanged(app, event) %Roll is the name of the button
FirstValue= randi([1, 10],1,1);
app.FirstText.Value= FirstValue; %FirstText is the numeric field text box that the answer will be shown at.
SecondValue= randi([1, 10],1,1);
app.SecondText.Value= SecondValue; %SecondText is the numeric field text box that the answer will be shown at.
ThirdValue= randi([1, 10],1,1);
app.ThirdText.Value= ThirdValue; %ThirdText is the numeric field text box that the answer will be shown at.
FourthValue= randi([1, 10],1,1);
app.FourthText.Value= FourthValue; %FourthText is the numeric field text box that the answer will be shown at.
  4 个评论
Mohammad Sami
Mohammad Sami 2020-11-24
I meant what you want to happen to the first value when you click the second time. So for the second time onwards, do you want to keep the values generated previously ? What should happen when you click the 5th time ?
Sean
Sean 2020-11-24
I want the first value to remain the same and yeah after the second time onwards it keeps the previously generated number. Also when you click the button 5 or times nothing will happen. You can only click four times to generate the four numbers.

请先登录,再进行评论。

回答(1 个)

Mohammad Sami
Mohammad Sami 2020-11-25
Something like this should work. it assumes the initial values are set to 0.
function RollValueChanged(app, event) %Roll is the name of the button
Value= randi([1, 10],1,1);
switch true
case app.FirstText.Value == 0
app.FirstText.Value = Value;
case app.SecondText.Value == 0
app.SecondText.Value = Value;
case app.ThirdText.Value == 0
app.ThirdText.Value = Value;
case app.FourthText.Value == 0
app.FourthText.Value = Value;
otherwise
% do nothing
end
end
  2 个评论
Sean
Sean 2020-11-27
Can you explain why we would use a switch here and why set the switch to true? The function works, but I don't understand why it works. Thank you in advance!
Mohammad Sami
Mohammad Sami 2020-11-27
It's not necessary to do it this way. You can essentially do the same with if else statements.
The switch statement will evaluate the case statements and it will execute the one that is true. If there are multiple cases that are true, it will only execute the first one that is true.
There is some discussion on this topic on the thread here. You can see if you want to implement this with if else.
https://www.mathworks.com/matlabcentral/answers/12290-problem-using-switch-with-logical-operators-larger-than-do-this-or-less-than-do-this
There are still other ways to implement this besides the above 2.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by