GUI Interface to guess a random number

3 次查看(过去 30 天)
Hi guys.
I'm trying to make a program interface where the user can input a value and guess a random generated number between 1 an 100.
I got a problem because I don't know where to put the randi generator. If i put it inside the callback, then anytime the button is pressed, a new number will be generated.
Can anyone help me?
This is my code:
function varargout = Adivinar(varargin)
% ADIVINAR MATLAB code for Adivinar.fig
% ADIVINAR, by itself, creates a new ADIVINAR or raises the existing
% singleton*.
%
% H = ADIVINAR returns the handle to a new ADIVINAR or the handle to
% the existing singleton*.
%
% ADIVINAR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ADIVINAR.M with the given input arguments.
%
% ADIVINAR('Property','Value',...) creates a new ADIVINAR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Adivinar_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Adivinar_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Adivinar
% Last Modified by GUIDE v2.5 27-May-2020 13:49:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Adivinar_OpeningFcn, ...
'gui_OutputFcn', @Adivinar_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Adivinar is made visible.
function Adivinar_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Adivinar (see VARARGIN)
% Choose default command line output for Adivinar
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Adivinar wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Adivinar_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function random (hObject, eventdata, handles)
your_variable= 10
handles.your_variable=your_variable
guidata(hObject,handles)
function input_Callback(hObject, eventdata, handles)
% hObject handle to input (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of input as text
% str2double(get(hObject,'String')) returns contents of input as a double
% --- Executes during object creation, after setting all properties.
function input_CreateFcn(hObject, eventdata, handles)
% hObject handle to input (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in botonadivinar.
function botonadivinar_Callback(hObject, eventdata, handles)
random=handles.your_variable
inp = get(handles.input,'String');
numero = str2double(inp);
if numero<random
set(handles.aviso,'String', 'El numero ingresado es menor.')
end
if numero>random
set(handles.aviso,'String', 'El numero ingresado es mayor.')
end
if numero==random
set(handles.aviso,'String', 'Felicidades. Usted adivino el numero.')
end
inp1 = get(handles.intentos,'String');
conteo = str2double(inp1);
conteo=conteo+1;
set(handles.intentos,'String', conteo)
% hObject handle to botonadivinar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

回答(1 个)

Rik
Rik 2020-5-27
编辑:Rik 2020-5-27
You should put it in the function that executes when you start you GUI: OpeningFcn.
Be sure to store the value in the handles struct.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by