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

2020/6/5

How to take user input from box and turn it into a variable

Sammyy2323 Sammyy2323

2020/6/5

#
So this is mainly for danpost i was looking at one of your older discussions and I saw the one with
1
String inputValue = JOptionPane.showInputDialog("Please input a value");
I want to make it so say an integer variable called amount = 6. If the user inputs 6 into the box, then they move on to a you win screen, if they put in say 20, its goes to a different screen. Need help project is due in about 5 hours.
danpost danpost

2020/6/5

#
Sammyy2323 wrote...
So this is mainly for danpost i was looking at one of your older discussions and I saw the one with << Code Omitted >>
Wow. That code is outdated.
I want to make it so say an integer variable called amount = 6. If the user inputs 6 into the box, then they move on to a you win screen, if they put in say 20, its goes to a different screen. Need help project is due in about 5 hours.
You can use:
1
String input = Greenfoot.ask("Please input a value");
to get the input.
Sammyy2323 Sammyy2323

2020/6/6

#
so how would i take the input and set it equal to the amount variable to see if the input equals the amount of balls in the world? If you need to I can show code.
Sammyy2323 Sammyy2323

2020/6/6

#
why isn't this working?
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;
/**
 * Write a description of class Answer4 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Answer4 extends Actor
{
    public int answer;
    public int correct;
    /**
     * Act - do whatever the Answer4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if ( Greenfoot.mouseClicked(this) )
        {
            String input = Greenfoot.ask("How many balls were there?");
            answer = Integer.valueOf(input);
 
        }
    }  
 
    public void correct(int Amountofballs)
    {
        if( answer == Amountofballs)
        {
            answer= correct;
        }
    }
 
    public void Winner()
    {
        if( answer == correct)
        {
            Greenfoot.setWorld(new WinScreen());
        }
    }
}
MyWorld class and the question is being asked on a different screen btw
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Int array is created to make random amount of balls in the world, when the
 * balls are created, they are randomly put into the world.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    public int Amountofballs;
    /**
     * Size of the world
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
 
    /**
     * This is where the balls are created form the int array. Also, timer
     * and other objects (like the text TIME LEFT) are created
     */
    private void prepare()
    {
        CreateBalls();
        Timer timer = new Timer();
        addObject(timer,414,72);
        timer.setLocation(399,69);
        timer.setLocation(843,288);
        timer.setLocation(400,69);
        removeObject(timer);
 
        addObject(timer,375,110);
        timer.setLocation(384,61);
        Text text = new Text();
        addObject(text,388,33);
    }
     
    /**
     * Int array and 2 for loops to create the random amount of balls in the
     * world, and also the random placement of the balls in the world.
     */
    public void CreateBalls()
    {
         
        int [] num = new int [4];
        num [0] = 1+Greenfoot.getRandomNumber(2);
        num [1] = 1+Greenfoot.getRandomNumber(2);
        num [2] = 1+Greenfoot.getRandomNumber(2);
        num [3] = 1+Greenfoot.getRandomNumber(2);
         
        for (int i=0; i<num.length; i++)
         
        {
             
            for (int n=0; n<num[i]; n++)
            {
                 
                int randomX = Greenfoot.getRandomNumber(getWidth()-50)+25;
                int randomY = Greenfoot.getRandomNumber(getHeight()-50)+25;
                Ball0 ball = new Ball0(i);
                addObject(ball, randomX,randomY);
                Amountofballs = num[0] + num[1] + num[2] + num[3];
            }
             
        }
    }
     
}
danpost danpost

2020/6/6

#
Where are you calling correct and Winner from in Answer4 class?
Sammyy2323 Sammyy2323

2020/6/6

#
why would i need to call it if I defined them in the Answer4 class? Im only using them in the Answer4 class
danpost danpost

2020/6/6

#
Sammyy2323 wrote...
why would i need to call it if I defined them in the Answer4 class? Im only using them in the Answer4 class
Because no method executes without being invoked. Certain methods, like act, may appear to execute without being called, but it, as well as a few others, are invoked by the underlying greenfoot platform. Even your initial world is instantiated internally by greenfoot.
Sammyy2323 Sammyy2323

2020/6/6

#
so what do i need to do for this to work?
danpost danpost

2020/6/6

#
Sammyy2323 wrote...
so what do i need to do for this to work?
How do you get to the answer screen where the Answer4 actor is?
Sammyy2323 Sammyy2323

2020/6/6

#
It didn't work, maybe i did something wrong here is the code for Myworld, Ball0, and Answer4
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Int array is created to make random amount of balls in the world, when the
 * balls are created, they are randomly put into the world.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    /**
     * Size of the world
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
 
    /**
     * This is where the balls are created form the int array. Also, timer
     * and other objects (like the text TIME LEFT) are created
     */
    private void prepare()
    {
        CreateBalls();
        Timer timer = new Timer();
        addObject(timer,375,110);
        timer.setLocation(384,61);
        Text text = new Text();
        addObject(text,388,33);
    }
     
    /**
     * Int array and 2 for loops to create the random amount of balls in the
     * world, and also the random placement of the balls in the world.
     */
    public void CreateBalls()
    {
         
        int [] num = new int [4];
        num [0] = 1+Greenfoot.getRandomNumber(2);
        num [1] = 1+Greenfoot.getRandomNumber(2);
        num [2] = 1+Greenfoot.getRandomNumber(2);
        num [3] = 1+Greenfoot.getRandomNumber(2);
         
        for (int i=0; i<num.length; i++)
         
        {          
            for (int n=0; n<num[i]; n++)
            {               
                int randomX = Greenfoot.getRandomNumber(getWidth()-50)+25;
                int randomY = Greenfoot.getRandomNumber(getHeight()-50)+25;
                Ball0 ball = new Ball0(i);
                addObject(ball, randomX,randomY);
            }
             
        }
    }
     
}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
 
/**
 * Main ball class where the string array is created for the random balls to
 * be placed in the world. Also, the ball moves and randomly bounces when
 * it hits an edge.
 *
 * Sam Izakson
 * @version (a version number or a date)
 */
public class Ball0 extends Actor
{
    /**
     * string array to make the colors random
     */
    private static final String [] Colors = {"RED", "BLUE", "GREEN", "YELLOW"};
    private int value;
    /**
     * takes the output from the array and places the value + ball.png to
     * produce a random color ball
     */
    public Ball0(int id)
    {
        value = id;
        setImage(new GreenfootImage(Colors[value] +"-ball.png"));
    }
 
    /**
     * Returns the array value
     */
    public String getColor()
    {
        return Colors[value];
    }
 
    /**
     * Ball moves around the world, if it is at edge, the ball will bounce off
     * the wall
     */
    public void act()
    {
        move(10);
        if (atWorldEdge())
        {
            turn(25);
        }   
    }
 
    /**
     * When the ball reaches the edge of the world,
     * the ball will bounce randomly in a direction.
     */
    public boolean atWorldEdge()
    {
        if( getX() <20 || getX() > getWorld().getWidth() -20)
            return true;
        if( getY() <20 || getY() > getWorld().getHeight() -20)
            return true;
        else
            return false;
    }   
 
}
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Answer4 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Answer4 extends Actor
{
    public int answer = 0;
    /**
     * Act - do whatever the Answer4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if ( Greenfoot.mouseClicked(this) )
        {
            String input = Greenfoot.ask("How many balls were there?");
            answer = Integer.valueOf(input);
 
         
        if( answer == getWorld().getObjects(Ball0.class).size())
        {
           Greenfoot.setWorld(new WinScreen());
        }
    }
}
}
danpost danpost

2020/6/6

#
I still do not see where any Answer4 object is being created or added into any world where it can get clicked on.
Sammyy2323 Sammyy2323

2020/6/6

#
Oh i have a different world where when the Timer reaches 0 on the Myworld screen then its switches to a new world where the Answer4 is on a new screen
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Choosing here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Choosing extends World
{
 
    /**
     * Constructor for objects of class Choosing.
     *
     */
    public Choosing()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 500, 1);
 
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Question question = new Question();
        addObject(question,326,214);
        question.setLocation(238,31);
        Question question2 = new Question();
        addObject(question2,469,201);
        question.setLocation(204,152);
        question.setLocation(99,152);
        Ball1 ball1 = new Ball1();
        addObject(ball1,99,152);
        question.setLocation(234,458);
        question.setLocation(133,405);
        ball1.setLocation(142,431);
        question.setLocation(165,234);
        removeObject(question);
        Ball3 ball3 = new Ball3();
        addObject(ball3,143,703);
        Ball2 ball2 = new Ball2();
        addObject(ball2,136,838);      
        ball1.setLocation(167,435);
        removeObject(ball1);
        removeObject(ball3);
        removeObject(ball2);
        addObject(ball3,150,705);
        removeObject(ball3);
        Answer4 answer4 = new Answer4();
        addObject(answer4,129,202);
        question2.setLocation(321,203);
        question2.setLocation(522,204);
        question2.setLocation(326,377);              
        question2.setLocation(523,396);              
        question2.setLocation(526,765);
        question2.setLocation(348,761);
        question2.setLocation(366,585);
        question2.setLocation(552,572);
        question2.setLocation(361,458);
        answer4.setLocation(144,444);
        question2.setLocation(399,110);
        question2.setLocation(433,43);           
        question2.setLocation(363,534);
        answer4.setLocation(192,541);
        question2.setLocation(795,90);
        removeObject(question2);
        Question question3 = new Question();
        addObject(question3,485,191); 
        answer4.setLocation(479,406);
        question3.setLocation(453,20);
        answer4.setLocation(394,212);
        question3.setLocation(359,318);
        question3.setLocation(387,53);
        answer4.setLocation(403,199);
        Question question4 = new Question();
        addObject(question4,403,199);
        answer4.setLocation(402,191);
        answer4.setLocation(458,656);
        removeObject(question4);
        answer4.setLocation(392,331);
        removeObject(question3);
        answer4.setLocation(374,446);
        Question question5 = new Question();
        addObject(question5,400,201);
    }
 
    public void Text()
    {
        GreenfootImage text = new GreenfootImage("", 22, Color.BLACK,Color.WHITE);
    }
}
This screen is where the user will click on the Answer4 object and puts the input in
danpost danpost

2020/6/6

#
Okay. Now I can see how to code it. In MyWorld class, insert at line 12:
1
public static int ballCount;
and at line 29 (to be 2nd line in prepare method):
1
ballCount = numberOfObjects();
Now, in the Answer4 class, you can use:
1
if (answer == MyWorld.ballCount)
You need to login to post a reply.