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
int answer = -1; while (answer != A[qSet][number] && reply == JOptionPane.YES_OPTION)
if (options.length() < 2)
if ("".equals(options))int numA = Greenfoot.getRandomNumber(10); int numB = Greenfoot.getRandomNumber(10); String question = ""+ numA + "+" + numB; int answer = numA + numB;
// 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 + ":";// add the following methods to the enemy class
public String getQuestion()
{
return question;
}
public int getAnswer()
{
return answer;
}// 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
}
}
}