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

2014/8/19

Maths Game

1
2
Megrr Megrr

2014/8/19

#
I am currently making a Maths game aimed at students in very early stages of school (grade prep to three) so they can learn basic maths and colours. I cannot figure out how to change levels using an "If clicked" statement how I want to. Is there any possibility someone might know how or even an easier way?
davmac davmac

2014/8/19

#
Usually something like:
if (Greenfoot.mouseClicked(this)) {
   Greenfoot.setWorld(new NextLevelWorld()); // use appropriate class name
}
Megrr Megrr

2014/8/24

#
Thank you. That helped my project a lot.
Megrr Megrr

2014/8/28

#
Trying to let the user know if they got the correct answer for a level or not. Is it incorrect coding? This is what I have
import javax.swing.JOptionPane;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class text_boxAns extends Texts
{
    public int var_Answer;
    private static int Lvl = 1;
    private static int Red;
    private static int Blue;
    private static int Green;
    private static int Orange;
    public void act() 
    {
        Enter_Answer();
        //Correction();
    }   
    public void Enter_Answer()
    {
        if(Greenfoot.mouseClicked(this))
        {
            var_Answer = Integer.parseInt(JOptionPane.showInputDialog("Please enter Your Answer"));
            if (Lvl == 4){
                if (var_Answer == 10)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 5){
                if (var_Answer == 3)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 6){
                if (var_Answer == 9)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 7){
                if (var_Answer == 6)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 10){
                if (var_Answer == 1)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 11){
                if (var_Answer == 1)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 12){
                if (var_Answer == 1)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 13){
                if (var_Answer == 1)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 16){
                if (var_Answer == Red)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 17){
                if (var_Answer == Red)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 18){
                if (var_Answer == Red)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
            if (Lvl == 19){
                if (var_Answer == Red)
                {
                    System.out.println("That is Correct!");
                }
                else
                {
                    System.out.println("That is Wrong!");
                }
            }
        }
    }
}
danpost danpost

2014/8/28

#
If it does what you want it to and there are no errors or adverse behaviors, then it is not 'bad' coding. Some coding may be more structured, or more efficient, or it may be more detailed and spelt out. The excessive use of 'if, if-else' seems a bit much considering each does pretty much the same thing. Using an array or setting a field to the correct answer might help to reduce the code to one 'if-else' block. For example:
// with array ('0 for answers unused or unknown to me)
public static final int[] answers = { 0, 0, 0, 0, 10, 3, 9, 6, 0, 0, 1, 1, 1, 1, 0, 0, Red, Red, Red, Red };
// then you could just use the following one time
if (var_Answer == answers[Lvl])
{
    System.out.println("That is Correct!");
}
else
{
    System.out.println("That is Wrong!");
}
Megrr Megrr

2014/8/30

#
This code isn't working. Did I need to relocate bits of it? Or am I thinking too hard and overdoing it?
danpost danpost

2014/8/30

#
You will need to re-post your class code to show what you currently have.
Super_Hippo Super_Hippo

2014/8/30

#
You only have to place both pieces of his code in the correct position. So the first part on top and the second part inside the 'Enter_Answer' method.
Megrr Megrr

2014/9/2

#
When I entered the level I need to enter an answer all I got was a Greenfoot terminal that constantly printed "That is wrong!" before I entered anything.
import javax.swing.JOptionPane;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class text_boxAns extends Texts
{
    public int var_Answer;
    private static int Lvl = 1;
    private static int Red;
    private static int Blue;
    private static int Green;
    private static int Orange;
    // with array ('0 for answers unused or unknown to me)  
    public static final int[] answers = { 0, 0, 0, 0, 10, 3, 9, 6, 0, 0, 1, 1, 1, 1, 0, 0, Red, Red, Red, Red };  
    public void act() 
    {
        //Enter_Answer();
    }   
    public void Enter_Answer()
    {
        if(Greenfoot.mouseClicked(this))
        {
            // then you could just use the following one time  
            if (var_Answer == answers[Lvl])  
            {  
                System.out.println("That is Correct!");  
            }  
            else  
            {  
                System.out.println("That is Wrong!");  
            }  
        }
        }
    }
Megrr Megrr

2014/9/2

#
Never mind the previous question. I figured that one out. How do I line up the answers properly? I am being told the answer is wrong when it is correct.
public static final int[] answers = { 10, 3, 9, 6, 1, 1, 0, 9, 6, Red, Red, Red, Red };  
    public void act() 
    {
        Enter_Answer();
    }   
    public void Enter_Answer()
    {
        if(Greenfoot.mouseClicked(this))
        {
            var_Answer = Integer.parseInt(JOptionPane.showInputDialog("Please enter Your Answer"));
            if (var_Answer == answers[Lvl])  
            {  
                System.out.println("That is Correct!");  
            }  
            else  
            {  
                System.out.println("That is Wrong!");  
            }  
        }
        }
    }
Megrr Megrr

2014/9/2

#
And it isn't accepting words (Red - colour names)
danpost danpost

2014/9/2

#
Right now, as you have line 1 in your last code post, the correct answers are defined as: * Level 0: 10 * Level 1: 3 * Level 2: 9 * Level 3: 6 * etc. You must put the values in the array so that the answers are in the appropriate location for the level. As far as the fields with color names, 'Red', 'Green', 'Blue' and 'Orange', unless you assign a different value to them, their values will be zero (the default value for 'int' fields).
Megrr Megrr

2014/9/2

#
If I have levels that you don't put a answer in for do I exclude them since they don't have this class? And why won't my colour answers work?
danpost danpost

2014/9/2

#
You can insert zeroes for the levels that do not have answers (like I had given originally). You can also just not put anything in for them, like in the following:
public static final int[] answers = { ,,,, 10, 3, 9, 6,,, 1, 1, 1, 1,,, Red, Red, Red, Red };
There will still be values at those locations set at zero, anyway. I edited my last post to include your issue with the color-named 'int' fields.
Super_Hippo Super_Hippo

2014/9/2

#
If the answer to your question (in the game) is actually the color itself, then it isn't an integer. Then you couldn't use an array for your answers, but a list (because arrays can only handle one data type, not integer and strings for example). In this list, you place >"Red"< instead of >Red< then. (You should compare the strings cap-neutral then.) If your color names are meant to represent a number like you coded it (so you set Red, Blue and so on to a number somewhere), then ignore this post.
There are more replies on the next page.
1
2