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

2019/11/4

get random value from array based on level

comfortablyNumb comfortablyNumb

2019/11/4

#
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 :)
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
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);
         }
    }
1
2
3
4
5
6
7
8
/*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",};
Super_Hippo Super_Hippo

2019/11/4

#
To check if strings match, use the equals-method.
1
if (answers[questionNum].equals(answer))
To get a random number between 10 and 19 for example, you can do this:
1
10+Greenfoot.getRandomNumber(10);
comfortablyNumb comfortablyNumb

2019/11/4

#
ok ill add them now
comfortablyNumb comfortablyNumb

2019/11/4

#
thanks it works. didn't think of using the +10 for the array. But whats the difference between the .equals() method and the == operator
Super_Hippo Super_Hippo

2019/11/4

#
The equals method compares the characters/content in the string (true if they are the same), == is only true if they are both the same string (the same object). As a simple rule: Always use equals instead of == for strings. (Unless you actually want to check if it is the same object of course.)
You need to login to post a reply.