This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronzhi
ronzhi wrote ...

2013/4/8

non-repeating random number help

1
2
ronzhi ronzhi

2013/4/11

#
that's great how you loop the JDialog but one problem,if we answer corrrectly it will never stop asking while if wrong answer it will respawn as expected i tried change 'reply' but no luck
danpost danpost

2013/4/11

#
If you never have negative answers then an easy solution is to move the 'int answer = 0;' statement up above the 'while' statement changing its initial value to negative one, and add the condition that the answer be wrong to continue the loop:
int answer = -1;
while (answer != A[qSet][number] && reply == JOptionPane.YES_OPTION)
Using opposites, the 'while' statement now says to exit the loop if either the answer is correct or the user refused to retry.
danpost danpost

2013/4/11

#
If you allow negative answers, then you will need to declare a boolean before the loop ('answeredCorrectly') and add that be false in the while condition and set it to true when answer is correct.
ronzhi ronzhi

2013/4/11

#
that works danpost,but ehmm it still produce same question when i print the last 'options' length is never less than 5 don't know if that what you expected but i think 'options' length will never get < 2 which i don't know if it's the problem or not
danpost danpost

2013/4/11

#
That is probably due to poor communication. In your initial post, you wrote the following: "i want to create enemies that pop out 5 random questions out of 6 and can't be taken twice" This told me that you wanted one of the questions to be skipped each time (at random). So, after the fifth question, in my code, the question set is changed with six new questions of which, again, five will be selected at random.
danpost danpost

2013/4/11

#
If you want all questions to be asked, change the first line from:
if (options.length() < 2)
to
if ("".equals(options))
ronzhi ronzhi

2013/4/11

#
my bad,i'm so sorry i mean in my game there is 5 enemies that pop out question and i list the question set as many as i want like 6 or 10,but only 5 that will be use those 5 are taken randomly and can not be taken twice if it's already answered correctly would you help me again danpost?
ronzhi ronzhi

2013/4/11

#
still no luck
danpost danpost

2013/4/11

#
You must realize we are basically starting over again. That any code previously given/used for determining questions should be removed. From what I am gathering, now, is that you have 5 enemies that will present questions. And you are only asking 5 questions (from a multitude of questions) among those enemies. 5 questions for 5 enemies means one question per enemy. It may be better just to create the questions on the fly as you create the enemies. You can create a question with its answer as follows:
int numA = Greenfoot.getRandomNumber(10);
int numB = Greenfoot.getRandomNumber(10);
String question = ""+ numA + "+" + numB;
int answer = numA + numB;
I was thinking that if you kept track of the questions created, you can avoid duplicates. So, next is to start a String that will contain all the questions that are invalid (or already used) and make sure any new question is not in that String.
// all code given goes in enemy class
// add these instance fields
private static String fails = ":0+0:";
private String question="0+0";
private int answer;
// in the enemy class constructor, put the following
while(fails.indexOf(":"+question+":") >= 0)
{
    int numA = Greenfoot.getRandomNumber(10);
    int numB = Greenfoot.getRandomNumber(10);
    question = "" + numA + "+" + numB;
    answer = numA + numB;
}
fails += question + ":";
You could create a string of characters for your 'question' in reverse order and add that to 'fails' also, so you do not, for example, end up with both "1+2" and "2+1" as two of the given questions. The only other thing you will need is a way to export the values of 'question' and 'answer' to other classes.
// add the following methods to the enemy class
public String getQuestion()
{
    return question;
}

public int getAnswer()
{
    return answer;
}
ronzhi ronzhi

2013/4/11

#
i'm planning the question is not just math,maybe geography for next level that's why i want to list the questions set can i use your JDialog loop and double array with that condition? it really ease the pain maybe shuffle the string options '123456' and then trim the char when answered correctly
danpost danpost

2013/4/11

#
Using the '123456' method is good for a very limited number of questions per set (9 -- to be precise). If you decide to add more questions later everything will have to be revised. Better to prepare for that possibility now. OK, since not all answer are going to be numeric, we should change 'answer' to type String instead of int. Change the return type in the 'getAnswer' method to String also. You could, if you wanted to, still use the method above for determining the arithmetic questions (saving 'answer = ""+numA+numB'). Otherwise, the following should work for other sets of questions:
// add the class fields
private static final String[][] Q = { 
    { "First question for set one", "Second question for set one" },
    { "First question for set two", "Second question for set two" } };
private static final String[][] A = {
    { "First answer for set one", "Second answer for set one" },
    { "First answer for set two", "Second answer for set two" } };
private static String fails = ":0+0:";
private static int qSet;
private static int throws;
// throwing the question
private void throwQuestion()
{
    String q = "";
    String a = "";
    while(fails.indexOf(":"+q+":") >= 0)
    {
        if (qSet == 0)
        {
            int numA = Greenfoot.getRandomNumber(10);
            int numB = Greenfoot.getRandomNumber(10);
            q = "" + numA + "+" + numB;
            a = "" + numA + numB;
        }
        else
        {
            int rand = Greenfoot.getRandomNumber(Q[qSet].length);
            q = Q[qSet-1][rand];
            a = A[qSet-1][rand];
        }
    }
    boolean answeredCorrectly = false;
    // while loop for JPanel here; use
    //     if (a.equals(answer))
    // instead of parsing integer, etc. 
    // After exiting while loop
    if (answeredCorrectly)
    {
        fails += a;
        throws++;
        if (throws == 5)
        {
            fails = ":0+0:";
            throws = 0;
            qSet++;
            // if (qSet == MAX_LEVELS) gameover
        }
    }
 }
I did this fairly quickly, so I do not know how accurate it is. But I do need to go somewhere and was in a bit of a rush. Hope it helps.
ronzhi ronzhi

2013/4/17

#
Hi danpost,due to many things that i have to do i just fixed the question for certain number while still use your double array and JDialog loop but you've been really great help danpost thanks !
You need to login to post a reply.
1
2