与for循环中的数组进行Int比较,得不到重复值

时间:2014-08-12 13:28:36

标签: java arrays for-loop android-activity

我正在尝试确保在Android上的数学应用中不会重复生成的答案。这是我的代码: 如果我用这个数据初始化一个人物对象数组

private int answer = 0, operator = 0, operand1 = 0, operand2 = 0;
private Random random;
private int AnswerCounter = 0;
private int[] numberList {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
private int[] ans =new int[50];
private String[] operators = {"+", "-", "x", "/"};

以下是我用于随机化和双重检查重复答案的方法。 ans []似乎没有工作......

这可能是一个非常愚蠢的逻辑问题,但我一直在把头发拉到这里,但是无法解决这个问题。当我运行该程序时,答案仍然超过30,并且仍然有重复的答案。

感谢您寻找:)

private void chooseQuestion(){//if answer is repeated, operands and operators should be randomized again.

        operator = random.nextInt(operators.length);
        //choose operands
        operand1 = random.nextInt(numberList.length);
        operand2 = random.nextInt(numberList.length);

        //checks for operators
        if(operator==0){//ADD_OPERATOR
            answer = operand1+operand2;

            if(AnswerCounter==0){//first ans
                while(answer>30){
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1+operand2;
                }
            }

            else{//from the 2nd ans onwards
                for (int x=0; x<(AnswerCounter);x++){
                    while((answer==ans[x])||(answer>30)){
                        //choose operands
                        operand1 = random.nextInt(numberList.length);
                        operand2 = random.nextInt(numberList.length);
                        answer = operand1+operand2;
                    }
                }
            }
          ans[AnswerCounter] = answer;
          AnswerCounter++;//increase so that the array num can increase 
        }
        else if(operator==1){ //SUBTRACT_OPERATOR
            answer = operand1-operand2;

            if(AnswerCounter==0){//first ans
                while((operand2>operand1)||answer>30){//no negative answer allowed, answer cannot be above 30
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1-operand2;
                }
            }

            else{//from the 2nd ans onwards
                for (int x=0; x<(AnswerCounter);x++){
                    while((operand2>operand1)||answer==ans[x]||(answer>30)){
                        //choose operands
                        operand1 = random.nextInt(numberList.length);
                        operand2 = random.nextInt(numberList.length);
                        answer = operand1+operand2;
                    }
                    }

            }
            answer = operand1-operand2;
            ans[AnswerCounter] = answer;
            AnswerCounter++;//increase so that the array num can increase
            }
        else if(operator==2){ //MULTIPLY_OPERATOR
            /*
            while(operand1>12||operand2>12){
                operand1 = random.nextInt(numberList.length);
                operand2 = random.nextInt(numberList.length);
            }
            //while(operand2>13){
                //operand2 = random.nextInt(numberList.length);
            //}
            */
            answer = operand1*operand2;

            if(AnswerCounter==0){//first ans
                while(answer>30){
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1+operand2;
                }
            }
            else{//from the 2nd ans onwards
                for (int x=0; x<(AnswerCounter);x++){
                    while((operand1>12||operand2>12)||answer==ans[x]||(answer>30)){
                    //choose operands
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1*operand2;
                }
                }
            }
            answer = operand1*operand2;
            ans[AnswerCounter] = answer;
            AnswerCounter++;//increase so that the array num can increase
        }
        else if(operator==3){ //DIVIDE_OPERATOR
            /*
            //whole numbers only
            while((((double)operand1/(double)operand2)%1 > 0) 
                    || (operand1==operand2)||(operand1==0)||(operand2==0)){
                operand1 = random.nextInt(numberList.length);
                operand2 = random.nextInt(numberList.length);
            }
            */

            //answer = operand1/operand2;// not sure if this is an error
            if(AnswerCounter==0){//first ans
                while((((double)operand1/(double)operand2)%1 > 0)||(operand1==operand2)||(operand1==0)||answer>30){
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1/operand2;
                }
            }

            else{//from the 2nd ans onwards
            for (int x=0; x<(AnswerCounter);x++){

                while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2)||(operand1==0)||answer==ans[x]||(answer>30)){
                    //choose operands
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1/operand2;

                    try {//||(operand2==0)
                        operand1 = random.nextInt(numberList.length);
                        operand2 = random.nextInt(numberList.length);
                        answer = operand1/operand2;
                        } catch(ArithmeticException ex) {
                            operand2+=1;
                        }

                }

            }
            answer = operand1/operand2;
            ans[AnswerCounter] = answer;
            AnswerCounter++;//increase so that the array num can increase
        }

        }

2 个答案:

答案 0 :(得分:0)

您可以使用不允许重复元素的HashSet来存储答案。因此,如果您尝试在集合中添加答案并返回false,则表示它是重复的。

答案 1 :(得分:0)

假设AnswerCounter为3,ans []包含[2,4,6],然后下一个运算符为0,所以添加。此外,operand1为5,operand2为1.因此,代码表示:

            for (int x=0; x<(AnswerCounter);x++){
                while((answer==ans[x])||(answer>30)){
                    //choose operands
                    operand1 = random.nextInt(numberList.length);
                    operand2 = random.nextInt(numberList.length);
                    answer = operand1+operand2;
                }
            }

X = 0且1次传递OK,但接着是x == 2,条件失败,因为answer(6)== ans [x]因此必须选择另一个数字。也许这次它选择operand1和operand2为2.这个时间回答== 4,它不等于ans [x],所以条件通过并且循环完成,所以ans []变为[2,4, 6,4]。

要解决这个问题,请按照以下几点思考:检查一个值是否重复是一个逻辑&#34; chunk&#34;或&#34;单位&#34;工作 - 例如。你可以有一个功能&#34; isNotDuplicate&#34;。你会如何安排你的代码,以便只从一个地方调用这个函数?