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


1 2 | int answer = - 1 ; while (answer != A[qSet][number] && reply == JOptionPane.YES_OPTION) |
1 | if (options.length() < 2 ) |
1 | if ( "" .equals(options)) |
1 2 3 4 | int numA = Greenfoot.getRandomNumber( 10 ); int numB = Greenfoot.getRandomNumber( 10 ); String question = "" + numA + "+" + numB; int answer = numA + numB; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // 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 + ":" ; |
1 2 3 4 5 6 7 8 9 10 | // add the following methods to the enemy class public String getQuestion() { return question; } public int getAnswer() { return answer; } |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | // 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 } } } |