The code below is my method for what happens if the actor collides with the player. It should ask a question using Greenfoot.ask() and the user will return an answer. if that answer is correct it should +=1 to my score counter in the world. the problem is that the if statement does not accept the user input as correct. I think this is due to formatting in greenfoots.ask() but im not sure how to remove that in java.
Another problem is that im trying to generate the question from a random number which is used as an index to get the question from the array. However Greenfoot.getRandomNumber() has no way of specifying a range like x-y where x != 0.
summary
need to fix a problem where the if statement at line 29 does not see user input as == to the condition
need to specify a random number to get an array index generated in a range such as 10-19 or 20-29
i know i can do it if i use more arrays so if i had lvlxquestions and lvlxanswers. but i'd need six arrays. also i find this a more interesting way to do it :)
private void collideWithPlayer() { Actor playerOBJ = getOneIntersectingObject(player.class); //send question if(playerOBJ != null) { /*ask question if correct +1 to score if not do nothing * then remove object and +1 to questionsRemoved variable * once that is equal to a multiple of 10 increase lvl*/ switch(player.hints){ case 0: switch(GameWorld.lvlCounter.getValue()){ case 1: //get 0-9 value in the array if lvl 1 questionNum = Greenfoot.getRandomNumber(10); break; case 2: //get 10-19 value in the array if lvl 2 questionNum = Greenfoot.getRandomNumber(10, 20); break; case 3: //get 20-29 if lvl 3 questionNum = Greenfoot.getRandomNumber(20, 30); break; } //ask the questionNum and display the answer for debugging purposes answer = Greenfoot.ask(questions[questionNum] + " " + answers[questionNum]); //this if statement doesnt work even tho when the the input is the same as answers[questionNum] if(answer == answers[questionNum]){ GameWorld.score += 1; } break; default: //player has hints, so -1 hint and +1 score player.hints -= 1; GameWorld.score += 1; break; } //remove object World w = getWorld(); GameWorld.questionsRemoved += 1; w.removeObject(this); } }
/*here are the arrays these are just placeholder at the moment but i hope the purpose of this is clear*/ private String questions[] = {"l1", "l1","l1", "l1","l1", "l1","l1", "l1","l1", "l1", "l2", "l2", "l2", "l2", "l2", "l2", "l2", "l2", "l2", "l2", "l3", "l3", "l3", "l3", "l3", "l3", "l3", "l3", "l3", "l3",}; private String answers[] = {"a1", "a1", "a1", "a1", "a1", "a1", "a1", "a1", "a1", "a1", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a3", "a3", "a3", "a3", "a3", "a3", "a3", "a3", "a3", "a3",};