GuessingGame

package com.zzu.java12.linkedlisthw;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * 
 * @author Jeremy
 *猜数字游戏

1. 随机生成4个0到9的整数,组成一个序列(使用LinkedList<Integer>存储)
例如:3  6  4  4

2. 然后要求用户循环猜这4个数子,在用户每猜一次之后,提示用户有几个数字的位置正确了。
例如:用户输入3  7  2  4 (3和4的位置正确了),则提示用户有2个数字正确(但不提示具体是哪2个数字正确,需要用户自行判断)。

3. 使用LinkedList存储用户每次猜测的数字组合。用户可以随时查看之前自己的输入
例如:3  7  2  4
      5  7  2  4
      ……

4. 用户猜到第10次时,就没有机会,提示猜测失败。


注意:使用Math.random()方法获取0到1的随机数,然后扩大10倍,再转换成整型
形式:int a = (int)(Math.random() * 10);
      此时a就是随机数,依此方法获取4个随机数
 */
public class GuessingGame {
    public static void main (String[] args){

        // anwserList:存储随机生成的四个数的集合即anwser
        LinkedList<Integer> anwserList = new LinkedList<Integer>();

        //生成四个随机数,并将它们存储在anwserList集合中
        for(int i = 0 ; i<4; i++){
            anwserList.add(new Integer((int)(Math.random()*10)));
        }

        //遍历anwserList
        for(int i = 0 ; i<4 ; i++){
            System.out.println(anwserList.get(i));
        }

        //用户十次猜测机会
        for(int j = 10 ; j>0;j--){
            int sub = 0;//判断猜对了几个字
            Scanner scan = new Scanner(System.in);
            LinkedList<Integer> guessList = new LinkedList<Integer>();//存储将用户猜的数字的集合
            System.out.println("请输入四个数字");

            //用户输入四个数字,并存储在guessList集合中
            for(int i = 0 ; i<4; i++){
                guessList.add(new Integer(scan.nextInt()));
            }

            //没用的代码
            for(int i = 0 ; i<4; i++){
                System.out.println(guessList.get(i));
            }

            //比较用户输入的与随机生成的四个数字,并判断对了几个
            for(int i = 0; i<4;i++){
                if(guessList.get(i).equals(anwserList.get(i))){
                    sub = sub+1;
                }
            }

            System.out.println("您猜对了"+sub+"个");
            //对了四个,则结束循环
            if(sub==4){
                System.out.println("恭喜你猜对了");
                break;
            }
            //十次机会没有猜对,结束循环
            if(j==1){
                System.out.println("很遗憾");
                break;
            }
            System.out.println("您还有"+(j-1)+"次机会");
        }
    }   
}

注意:这道题我觉得很考验一个人的逻辑思维,所以逻辑思维不是很好的我就写了将近三个小时的时间,其中那十次循环中的sub,特别容易出问题,比如刚开始我就把它放在了循环外边,导致输出的sub为十次累加的对的次数,还有就是guessList集合,初始化的时候我也给放在了循环外,导致和anwserList集合一直是在和用户第一次输入的四个数字相比较。总之细心+认真才能更好更快的敲出高质量的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
function varargout = caishuzi(varargin) %CAISHUZI M-file for caishuzi.fig % CAISHUZI, by itself, creates a new CAISHUZI or raises the existing % singleton*. % % H = CAISHUZI returns the handle to a new CAISHUZI or the handle to % the existing singleton*. % % CAISHUZI('Property','Value',...) creates a new CAISHUZI using the % given property value pairs. Unrecognized properties are passed via % varargin to caishuzi_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*. % % CAISHUZI('CALLBACK') and CAISHUZI('CALLBACK',hObject,...) call the % local function named CALLBACK in CAISHUZI.M with the given input % arguments. % % *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 caishuzi % Last Modified by GUIDE v2.5 08-Mar-2008 22:02:40 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @caishuzi_OpeningFcn, ... 'gui_OutputFcn', @caishuzi_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 caishuzi is made visible. function caishuzi_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 unrecognized PropertyName/PropertyValue pairs from the % command line (see VARARGIN) % Choose default command line output for caishuzi handles.output = hObject; handles.source = rand4; %生成一个随机的四位数 handles.pc = 0; %用来保按‘ok’键的次数,但不成功 % Update handles structure guidata(hObject, handles); % UIWAIT makes caishuzi wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = caishuzi_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 edit_input_Callback(hObject, eventdata, handles) % hObject handle to edit_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 edit_input as text % str2double(get(hObject,'String')) returns contents of edit_input as a double handles.instr = str2double(get(hObject,'String')); %从编辑框取输入值 guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function edit_input_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_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 pushbutton_clo. function pushbutton_clo_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_clo (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) delete(handles.figure1) %关闭界面 % --- Executes on selection change in popupmenu. function popupmenu_Callback(hObject, eventdata, handles) % hObject handle to popupmenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = get(hObject,'String') returns popupmenu contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu handles.bushu = get(hObject,'value'); %获得指定步数限制,还未完成该功能 % --- Executes during object creation, after setting all properties. function popupmenu_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu 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 pushbutton_ok. function pushbutton_ok_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ok (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) A = handles.source; %A为生成的随机4位数 instr = handles.instr; %输入的4位数 handles.pc = handles.pc + 1; %按键次数 b = 0; %将输入的4位数拆开,组成矩阵B for i=1:4 B(5-i) = mod(instr,10); for j=1:4 if isequal(B(5-i),A(j)) %判断B与A是否有相同元素,并将其个数入b b=b+1; end end instr = fix(instr/10); end %判断输入是否合法 f=0; for i=1:4 C(1:4)=B(i); if sum(C==B)>=2 set(handles.text_rez, 'string','请重新输入4位不重复数!'); f=1; break end end if f==0 %输入合法 a=sum(A==B); %A与B的元素及其位置均相同的元素个数 b = b - a; %A与B的元素相同但位置不相同的元素个数 set(handles.text_rez, 'string',['第',num2str(handles.pc),'步: ',num2str(B),' ',... num2str(a),'A',num2str(b),'B']); %显示步骤及结果 end % --- Executes on button press in pushbutton_reset. function pushbutton_reset_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_reset (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.source = rand4; %重新生成一个随机4位数 guidata(hObject, handles); % --- Executes on button press in pushbutton_ans. function pushbutton_ans_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ans (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text_rez, 'string',['正确答案: ',num2str(handles.source)]); %显示正确答案 %定义生成随机4位数的函数 function Y=rand4() M=randperm(10); M(find(M==10))=0; Y=M(1:4);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值