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

2014/8/19

Maths Game

1
2
danpost danpost

2014/9/2

#
Super_Hippo wrote...
arrays can only handle one data type, not integer and strings for example
That is not exactly true. They can only be assigned one data type. Both 'Integer' and 'String' are subclasses of 'Object' and an array of type Object would allow ALL data types to be held within it. The following is an example of a valid array:
1
2
3
4
5
6
public Object[] objects =
{
    10, // an integer
    "Red", // a string
    java.awt.Color.BLUE // a color
};
Super_Hippo Super_Hippo

2014/9/2

#
Oh, interesting to know that you can just use Object as the type.
Megrr Megrr

2014/9/4

#
I am still not understanding how to fix the code. I am attempting to do what is suggested but it isn't working for me. Am I doing it wrong? This is the code I have so far: 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; private static int Purple; // with array ('0 for answers unused or unknown to me) public static final int answers = {10, 3, 9, 6, 1, 0, 9, 6, "Orange", "Green", "Blue", "Purple"}; 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) { System.out.println("That is Correct!"); } else { System.out.println("That is Wrong!"); } } } } The answers here aren't lining up with the answers that I do put into the Game still and it is getting confusing.
danpost danpost

2014/9/4

#
This line:
1
if (var_Answer == answers)
is trying to compare an 'int' value with an 'int' array. It needs to compare 'var_Answer' to only one of the elements in the 'answers' array (the answer for the current level). Something like this:
1
if (var_Answer == answers[Lvl])
Megrr Megrr

2014/9/11

#
I am having a similar problem with my var_Name. I have provided the coding for it here but I was wondering if it is the same problem as the var_Answer problem.
1
2
3
4
5
6
7
8
9
10
11
public void Enter_Name()
    {
        if(Greenfoot.mouseClicked(this))
        {
            var_Name = Integer.parseInt(JOptionPane.showInputDialog("Please enter Your Name"));
            if(var_Name == var_Name)
            {
                System.out.println("Hello " + var_Name);
            }
        }
    }
danpost danpost

2014/9/11

#
Line 6 will always be true (you are comparing a value to itself). Line 5 appears to be trying to convert an alpha String to an int value. 'Integer.parseInt' will not work on any alpha characters which your InputDialog message suggests would be returned to be parsed. If 'var_Name' is a String field, then you are also trying to assigned an int value to it in line 5.
Megrr Megrr

2014/9/11

#
how would I be able to code it so it can work then? I am not sure how to do that.
danpost danpost

2014/9/11

#
First, you need to show the line that declares the 'var_Name' field within the class.
davmac davmac

2014/9/11

#
Megrr, please use code tags for posting code. The code you posted above is not in code tags, and is wrong because the square brackets for arrays have been stripped out (plus it is harder to read, and harder to copy/paste). Use code tags.
danpost danpost

2014/9/11

#
Also, you have for some reason added String values to your 'answers' int array. This should cause a compilation error.
You need to login to post a reply.
1
2