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

2015/4/18

How to save a random number in a variable?

listiliz listiliz

2015/4/18

#
I am making an addition math game, which has two random variable on it. I made this code=
1
2
3
4
int a=Greenfoot.getRandomNumber(9);
    
    int b=Greenfoot.getRandomNumber(9);
int hasil=a+b;
but when i compile it, the "hasil" variable display wrong result. variable a is displaying 8 variable b is displaying 2 but the variable hasil is displaying 40. what should i do to save the random number so "hasil" can display the correct answer? Thank you
0gener 0gener

2015/4/18

#
Where are the variables a and b declared?
0gener 0gener

2015/4/18

#
I'll have to leave soon... but you probably declared the variables inside a method that was called in the act(). What's happening is that in every act cycle you will have a different a and a different b. So you should declare them as class variables:
1
2
private int randA = Greenfoot.getRandomNumber(9);
private int randB = Greenfoot.getRandomNumber(9);
and then the method:
1
2
3
4
5
6
public void displaySum()
{
     int result = a + b;
      
     getWorld().showText("" + result, getWorld().getWidth() / 2, getWorld().getHeight() / 2);     // To display in the center of the World.
}
danpost danpost

2015/4/18

#
The variable 'hasil' cannot be greater than '16' with the code you provided. There must be more coding somewhere that is changing the value of 'hasil'. Providing the code for the entire class would probably have been enough to see where the problem code was located.
listiliz listiliz

2015/4/21

#
hi, so here's the code
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
import greenfoot.*;
 
/**
 * Write a description of class makanan here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class makanan extends Actor
{
    
   int a = Greenfoot.getRandomNumber(9);
   int b = Greenfoot.getRandomNumber(9);
    int rand1=Greenfoot.getRandomNumber(40);
    int rand2=Greenfoot.getRandomNumber(40);
     
    int hasil= 0;
    /**
     * Act - do whatever the makanan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
this one is code for "a"
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
import greenfoot.*;
import java.awt.Color;
 
/**
 * Write a description of class penjumlah here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
 
public class penjumlah1 extends makanan
{
     
    public penjumlah1()
    {
        GreenfootImage img=new GreenfootImage(100,30);
         setImage(new GreenfootImage("" + a, 100, Color.WHITE, Color.BLACK));
 
    }
    /**
     * Act - do whatever the penjumlah wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
this one is code for variable "b"
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
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class penjumlah2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class penjumlah2 extends makanan
{
    public penjumlah2()
    {
        GreenfootImage img=new GreenfootImage(100,30);
         setImage(new GreenfootImage("" + b, 100, Color.WHITE, Color.BLACK));
 
    }
    /**
     * Act - do whatever the penjumlah2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
and this one is code for "result"
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
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class hasil here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class hasil extends makanan
{
    int hasil= a+b;
     
    public hasil()
    {
        GreenfootImage img=new GreenfootImage(30,30);
         setImage(new GreenfootImage("" + hasil, 24, Color.WHITE, Color.BLACK));
 
    }
    /**
     * Act - do whatever the hasil wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
please help me :)
Super_Hippo Super_Hippo

2015/4/21

#
I still don't know how a 40 can be displayed there, but I can tell you why the result won't be the correct one. Every object of the 'makanan' class or their subclasses get their own variables (so different numbers possible because they are random). If you use static variables, then every object will share the same variable:
1
2
protected static int a = Greenfoot.getRandomNumber(9);
protected static int b = Greenfoot.getRandomNumber(9);
listiliz listiliz

2015/4/21

#
0gener wrote...
Where are the variables a and b declared?
i declared it on the "makanan" penjumlah1= variable a penjumlah2=variable b hasil = result and i located it outside the act method :) is this wrong?
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
public class makanan extends Actor
{
    
   int a = Greenfoot.getRandomNumber(9);
   int b = Greenfoot.getRandomNumber(9);
    int rand1=Greenfoot.getRandomNumber(40);
    int rand2=Greenfoot.getRandomNumber(40);
     
    int hasil= 0;
    /**
     * Act - do whatever the makanan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}public class makanan extends Actor
{
    
   int a = Greenfoot.getRandomNumber(9);
   int b = Greenfoot.getRandomNumber(9);
    int rand1=Greenfoot.getRandomNumber(40);
    int rand2=Greenfoot.getRandomNumber(40);
     
    int hasil= 0;
    /**
     * Act - do whatever the makanan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
listiliz listiliz

2015/4/21

#
Super_Hippo wrote...
I still don't know how a 40 can be displayed there, but I can tell you why the result won't be the correct one.
1
2
protected static int a = Greenfoot.getRandomNumber(9);
protected static int b = Greenfoot.getRandomNumber(9);
Hi :) Super thank youu but now the game doesn't get a random number, it keeps displaying the same number everytime i reset, how can i fix this?
Super_Hippo Super_Hippo

2015/4/21

#
You could add this method to your makanan class.
1
2
3
4
5
public static void reset()
{
    a = Greenfoot.getRandomNumber(9);
    b = Greenfoot.getRandomNumber(9);
}
At the beginning of your world constructor (after the super call), you write this:
1
makanan.reset();
listiliz listiliz

2015/4/21

#
Super_Hippo wrote...
You could add this method to your makanan class.
1
2
3
4
5
public static void reset()
{
    a = Greenfoot.getRandomNumber(9);
    b = Greenfoot.getRandomNumber(9);
}
At the beginning of your world constructor (after the super call), you write this:
1
makanan.reset();
it works perfectly! thank you so much super_hippo!
danpost danpost

2015/4/21

#
I get the feeling that your class structure may be problematic. What does 'makanan' mean and how would you describe an object created from that class? However you describe it, if you cannot use the same exact words to describe all objects created from all of its subclasses, then those you cannot describe similarly should not subclass the 'makanan' class.
listiliz listiliz

2015/4/21

#
danpost wrote...
I get the feeling that your class structure may be problematic. What does 'makanan' mean and how would you describe an object created from that class? However you describe it, if you cannot use the same exact words to describe all objects created from all of its subclasses, then those you cannot describe similarly should not subclass the 'makanan' class.
ah yeah, i just realized that penjumlah 1, penjumlah 2 and hasil are not related to each other.. thanks for correcting me danpost..
You need to login to post a reply.