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

2015/5/5

Array comparison and Printing array problem

biferony biferony

2015/5/5

#
I am working on a project where I need to Print out two arrays and check what parts are the same. I tried using this code, but it didn't do anything.
if (Submitted_Answers[0] == Correct_Answers[0])
            {
                System.out.println("success");
            }
For the printing problem, I tired to just print out the (int) array, but the output was weird.
if (QuestionNumber == 20)
        {
            System.out.print (Submitted_Answers);
            System.out.print (Correct_Answers);
            
        }
danpost danpost

2015/5/6

#
What type of data do the array contain (how are the arrays declared)?
biferony biferony

2015/5/6

#
danpost wrote...
What type of data do the array contain (how are the arrays declared)?
They are integer arrays, they are declared like this:
int [] Correct_Answers = { 3, 2, 3, 2, 2, 1, 1, 3, 3, 2, 3, 1, 2, 3, 1, 1, 2, 3, 2, 3 };
int [] Submitted_Answers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
danpost danpost

2015/5/6

#
Okay. You have to compare each element in one array with its counterpart in the other array. You can use a 'for' loop and print out the value of the counter in each line of the print-out for easy referencing. Use the counter of the loop for the indecis of the arrays. You could even set up an int variable before the loop to count the number of correct (or wrong) submitted answers and print that out after the loop.
biferony biferony

2015/5/6

#
danpost wrote...
Okay. You have to compare each element in one array with its counterpart in the other array. You can use a 'for' loop and print out the value of the counter in each line of the print-out for easy referencing. Use the counter of the loop for the indecis of the arrays. You could even set up an int variable before the loop to count the number of correct (or wrong) submitted answers and print that out after the loop.
Can you give acctual code for these? I'm new to programming, so I don't know how to do all of those. If you want my full code, then you can see it below.
public class Clicker extends Actor
{
    int QuestionNumber = 1;
    int PercentCorrect = 0;
    
    int [] Correct_Answers = { 3, 2, 3, 2, 2, 1, 1, 3, 3, 2, 3, 1, 2, 3, 1, 1, 2, 3, 2, 3 };
    int [] Submitted_Answers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
        
    
    /**
     * Act - do whatever the Clicker wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Submitted_Answers[0] == Correct_Answers[0])
            {
                System.out.println("banana");
            }
       
       World Question1 = getWorld();
       GreenfootImage bg1 = new GreenfootImage("Question1.png");
       GreenfootImage bg2 = new GreenfootImage("Question2.png");
       GreenfootImage bg3 = new GreenfootImage("Question3.png");
       GreenfootImage bg4 = new GreenfootImage("Question4.png");
       GreenfootImage bg5 = new GreenfootImage("Question5.png");
       GreenfootImage bg6 = new GreenfootImage("Question6.png");
       GreenfootImage bg7 = new GreenfootImage("Question7.png");
       GreenfootImage bg8 = new GreenfootImage("Question8.png");
       GreenfootImage bg9 = new GreenfootImage("Question9.png");
       GreenfootImage bg10 = new GreenfootImage("Question10.png");
       GreenfootImage bg11 = new GreenfootImage("Question11.png");
       GreenfootImage bg12 = new GreenfootImage("Question12.png");
       GreenfootImage bg13 = new GreenfootImage("Question13.png");
       GreenfootImage bg14 = new GreenfootImage("Question14.png");
       GreenfootImage bg15 = new GreenfootImage("Question15.png");
       GreenfootImage bg16 = new GreenfootImage("Question16.png");
       if (QuestionNumber == 2)
        {
        Question1.setBackground(bg2);
        }
       if (QuestionNumber == 3)
        {
        Question1.setBackground(bg3);
        }
       if (QuestionNumber == 4)
        {
        Question1.setBackground(bg4);
        }
       if (QuestionNumber == 5)
        {
        Question1.setBackground(bg5);
        }
       if (QuestionNumber == 6)
        {
        Question1.setBackground(bg6);
        }
       if (QuestionNumber == 7)
        {
        Question1.setBackground(bg7);
        }
       if (QuestionNumber == 8)
        {
        Question1.setBackground(bg8);
        }
       if (QuestionNumber == 9)
        {
        Question1.setBackground(bg9);
        }
       if (QuestionNumber == 10)
        {
        Question1.setBackground(bg10);
        }if (QuestionNumber == 11)
        {
        Question1.setBackground(bg11);
        }
       if (QuestionNumber == 12)
        {
        Question1.setBackground(bg12);
        }
       if (QuestionNumber == 13)
        {
        Question1.setBackground(bg13);
        }
       if (QuestionNumber == 14)
        {
        Question1.setBackground(bg14);
        }
       if (QuestionNumber == 15)
        {
        Question1.setBackground(bg15);
        }
       if (QuestionNumber == 16)
        {
        Question1.setBackground(bg16);
        }
       
       
       if (QuestionNumber == 20)
        {
            System.out.println (Submitted_Answers);
            System.out.println (Correct_Answers);
            
        }
        
        //List QuestionCounter = getObjects(QuestionCounter.class);
        //if (QuestionCounter.size() == 2) addObject(new QuestionCounter(), 100, 100);
        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (Greenfoot.mouseClicked(null))
        {
            if (mouse.getX() >= 198 &&
                mouse.getX() <= 366 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                //Greenfoot.setWorld(new question4());
                QuestionNumber += 1;
                //System.out.println( QuestionNumber );
                   if (QuestionNumber == 1)
                    {
                        Submitted_Answers [0] = 1;
                        
                    }
                   if (QuestionNumber == 2)
                    {
                        Submitted_Answers [1] = 1;
                    }
                   if (QuestionNumber == 3)
                    {
                        Submitted_Answers [2] = 1;
                    }
                   if (QuestionNumber == 4)
                    {
                        Submitted_Answers [3] = 1;
                    }
                   if (QuestionNumber == 5)
                    {
                        Submitted_Answers [4] = 1;
                    }
                   if (QuestionNumber == 6)
                    {
                        Submitted_Answers [5] = 1;
                    }
                   if (QuestionNumber == 7)
                    {
                        Submitted_Answers [6] = 1;
                    }
                   if (QuestionNumber == 8)
                    {
                        Submitted_Answers [7] = 1;
                    }
                   if (QuestionNumber == 9)
                    {
                        Submitted_Answers [8] = 1;
                    }
                   if (QuestionNumber == 10)
                    {
                        Submitted_Answers [9] = 1;
                    }
                   if (QuestionNumber == 11)
                    {
                        Submitted_Answers [10] = 1;
                    }
                   if (QuestionNumber == 12)
                    {
                        Submitted_Answers [11] = 1;
                    }
                   if (QuestionNumber == 13)
                    {
                        Submitted_Answers [12] = 1;
                    }
                   if (QuestionNumber == 14)
                    {
                        Submitted_Answers [13] = 1;
                    }
                   if (QuestionNumber == 15)
                    {
                        Submitted_Answers [14] = 1;
                    }
                   if (QuestionNumber == 16)
                    {
                        Submitted_Answers [15] = 1;
                    }
                   if (QuestionNumber == 17)
                    {
                        Submitted_Answers [16] = 1;
                    }
                   if (QuestionNumber == 18)
                    {
                        Submitted_Answers [17] = 1;
                    }
                   if (QuestionNumber == 19)
                    {
                        Submitted_Answers [18] = 1;
                    }
                   if (QuestionNumber == 20)
                    {
                        Submitted_Answers [19] = 1;
                    }
                    
                   
            }
            
            if (mouse.getX() >= 481 &&
                mouse.getX() <= 649 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                //Greenfoot.setWorld(new question4());
                QuestionNumber += 1;
                //System.out.println( QuestionNumber );
                   if (QuestionNumber == 1)
                    {
                        Submitted_Answers [0] = 1;
                        
                    }
                   if (QuestionNumber == 2)
                    {
                        Submitted_Answers [1] = 2;
                    }
                   if (QuestionNumber == 3)
                    {
                        Submitted_Answers [2] = 2;
                    }
                   if (QuestionNumber == 4)
                    {
                        Submitted_Answers [3] = 2;
                    }
                   if (QuestionNumber == 5)
                    {
                        Submitted_Answers [4] = 2;
                    }
                   if (QuestionNumber == 6)
                    {
                        Submitted_Answers [5] = 2;
                    }
                   if (QuestionNumber == 7)
                    {
                        Submitted_Answers [6] = 2;
                    }
                   if (QuestionNumber == 8)
                    {
                        Submitted_Answers [7] = 2;
                    }
                   if (QuestionNumber == 9)
                    {
                        Submitted_Answers [8] = 2;
                    }
                   if (QuestionNumber == 10)
                    {
                        Submitted_Answers [9] = 2;
                    }
                   if (QuestionNumber == 11)
                    {
                        Submitted_Answers [10] = 2;
                    }
                   if (QuestionNumber == 12)
                    {
                        Submitted_Answers [11] = 2;
                    }
                   if (QuestionNumber == 13)
                    {
                        Submitted_Answers [12] = 2;
                    }
                   if (QuestionNumber == 14)
                    {
                        Submitted_Answers [13] = 2;
                    }
                   if (QuestionNumber == 15)
                    {
                        Submitted_Answers [14] = 2;
                    }
                   if (QuestionNumber == 16)
                    {
                        Submitted_Answers [15] = 2;
                    }
                   if (QuestionNumber == 17)
                    {
                        Submitted_Answers [16] = 2;
                    }
                   if (QuestionNumber == 18)
                    {
                        Submitted_Answers [17] = 2;
                    }
                   if (QuestionNumber == 19)
                    {
                        Submitted_Answers [18] = 2;
                    }
                   if (QuestionNumber == 20)
                    {
                        Submitted_Answers [19] = 2;
                    }
            }
            
            if (mouse.getX() >= 762 &&
                mouse.getX() <= 930 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                //Greenfoot.setWorld(new question4());
                QuestionNumber += 1;
                //System.out.println( QuestionNumber );
                   if (QuestionNumber == 1)
                    {
                        Submitted_Answers [0] = 3;
                        
                    }
                   if (QuestionNumber == 2)
                    {
                        Submitted_Answers [1] = 3;
                    }
                   if (QuestionNumber == 3)
                    {
                        Submitted_Answers [2] = 3;
                    }
                   if (QuestionNumber == 4)
                    {
                        Submitted_Answers [3] = 3;
                    }
                   if (QuestionNumber == 5)
                    {
                        Submitted_Answers [4] = 3;
                    }
                   if (QuestionNumber == 6)
                    {
                        Submitted_Answers [5] = 3;
                    }
                   if (QuestionNumber == 7)
                    {
                        Submitted_Answers [6] = 3;
                    }
                   if (QuestionNumber == 8)
                    {
                        Submitted_Answers [7] = 3;
                    }
                   if (QuestionNumber == 9)
                    {
                        Submitted_Answers [8] = 3;
                    }
                   if (QuestionNumber == 10)
                    {
                        Submitted_Answers [9] = 3;
                    }
                   if (QuestionNumber == 11)
                    {
                        Submitted_Answers [10] = 3;
                    }
                   if (QuestionNumber == 12)
                    {
                        Submitted_Answers [11] = 3;
                    }
                   if (QuestionNumber == 13)
                    {
                        Submitted_Answers [12] = 3;
                    }
                   if (QuestionNumber == 14)
                    {
                        Submitted_Answers [13] = 3;
                    }
                   if (QuestionNumber == 15)
                    {
                        Submitted_Answers [14] = 3;
                    }
                   if (QuestionNumber == 16)
                    {
                        Submitted_Answers [15] = 3;
                    }
                   if (QuestionNumber == 17)
                    {
                        Submitted_Answers [16] = 3;
                    }
                   if (QuestionNumber == 18)
                    {
                        Submitted_Answers [17] = 3;
                    }
                   if (QuestionNumber == 19)
                    {
                        Submitted_Answers [18] = 3;
                    }
                   if (QuestionNumber == 20)
                    {
                        Submitted_Answers [19] = 3;
                    }
            }
        }
    }    
}
danpost danpost

2015/5/6

#
Did you not think there might be an easier way to code the class? Repetitive similar code can usually be reduced to simple statements. This is what I see:
public class Clicker extends Actor
{
    int QuestionNumber;
    int PercentCorrect;
     
    int [] Correct_Answers = { 3, 2, 3, 2, 2, 1, 1, 3, 3, 2, 3, 1, 2, 3, 1, 1, 2, 3, 2, 3 };
    int [] Submitted_Answers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    public void act() 
    {
        if (Submitted_Answers[0] == Correct_Answers[0])
        {
            System.out.println("banana");
        }
        getWorld().setBackground("Question"+(QuestionNumber+1)+".png");
        if (QuestionNumber == 20)
        {
            System.out.println (Submitted_Answers);
            System.out.println (Correct_Answers);
        }
        //List QuestionCounter = getObjects(QuestionCounter.class);
        //if (QuestionCounter.size() == 2) addObject(new QuestionCounter(), 100, 100);
        if (Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            int clicked = 0;
            if (mouse.getX() >= 198 &&
                mouse.getX() <= 366 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                clicked = 1;
            }
            if (mouse.getX() >= 481 &&
                mouse.getX() <= 649 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                clicked = 2;
            }             
            if (mouse.getX() >= 762 &&
                mouse.getX() <= 930 &&
                mouse.getY() >= 452 &&
                mouse.getY() <= 548)
            {
                clicked = 3;
            }
            if (clicked > 0)
            {
                Submitted_Answers[QuestionNumber] = clicked;
                QuestionNumber++;
            }
        }
    }    
}
My next post will work off of this code (which is essentially the same as your code above).
danpost danpost

2015/5/6

#
Okay. Now, as soon as the first question is answered correctly, lines 11 through 14 will cause the terminal to print "banana" repeatedly and indefinitely. That is not what you want, so remove those lines. Line 15 will repeated set the appropriate image to the actor; yet, it only needs to be set each time a new question is posed. I presume the first question image is set as the default image for the class since you are not setting it in your code. Therefore, the image should be set after my line 51, where the question number is incremented. Place the following lines after line 51 of my code:
if (QuestionNumber < 20)
{
    getWorld().setBackground("Question"+(QuestionNumber+1)+".png");
}
and remove line 15. Lines 16 through 20 was your problem code where you were trying to print out the submitted answers with the correct answers. One problem with this is that it will execute prematurely (before the 20th question is answered). Another is it will print repeatedly, until the 20th question is answered and the value of QuestionNumber becomes 21. Even if you corrected the condition to (QuestionNumber == 21), it would then print indefinitely. You either need to change the value of QuestionNumber inside the code-block or (and this next option would be better) move these lines to after where QuestionNumber is incremented (my line 51). The inserted code given above in this post would then be:
if (QuestionNumber < 20)
{ // next question
    getWorld().setBackground("Question"+(QuestionNumber+1)+".png");
}
else
{ // all questions answered
    System.out.println (Submitted_Answers);
    System.out.println (Correct_Answers);
}
We will return to what should actually be in the 'else' code block later. Moving along, the rest of the code, lines 23 through 53, must not be allowed to execute once all questions are answered. Change line 23 to this:
if (QuestionNumber < 20 && Greenfoot.mouseClicked(null))
See if this helps and report any problems.
biferony biferony

2015/5/6

#
biferony biferony

2015/5/6

#
I did everything that you said, but I still have the problem that the output is repeating lines of this:
[I@134f1db
[I@f294c5
Do you know any way to fix that?
danpost danpost

2015/5/6

#
I did state the following about my lines 18 and 19:
We will return to what should actually be in the 'else' code block later.
Now, there are two way to go about printing your results to the terminal. You can print them all out after the last question is answered or you can print them out as the questions are being answered. Which way would you like to proceed with this? As a sidenote, the second way will eliminate the need to revisit the elements in the array for printing the results.
biferony biferony

2015/5/6

#
danpost wrote...
I did state the following about my lines 18 and 19: Now, there are two way to go about printing your results to the terminal. You can print them all out after the last question is answered or you can print them out as the questions are being answered. Which way would you like to proceed with this? As a sidenote, the second way will eliminate the need to revisit the elements in the array for printing the results.
It would be best if they are all printed out at the end. It would just be the more standard way for what I'm doing.
danpost danpost

2015/5/7

#
biferony wrote...
It would be best if they are all printed out at the end. It would just be the more standard way for what I'm doing.
Then replace lines 7 and 8 in my last code post with the appropriate code. Again, you can use a 'for' or 'while' loop to iterate through each element in the array to compare them and print out the appropriate text:
for (int n=0; n<20; n++)
{
    int qNum = n+1; // question number
    int submitted = Submitted_Answers[n]; // submitted answer to this question
    int actual = Correct_Answers[n]; // correct answer to this question
    boolean match = submitted == actual; // true if this question was answered correctly, else false
    System.out.print(" whatever you wish to output for this question ");
}
biferony biferony

2015/5/7

#
danpost wrote...
biferony wrote...
It would be best if they are all printed out at the end. It would just be the more standard way for what I'm doing.
Then replace lines 7 and 8 in my last code post with the appropriate code. Again, you can use a 'for' or 'while' loop to iterate through each element in the array to compare them and print out the appropriate text:
I did this, but it just printed out 20 of the phrase, even though the arrays only matched around five times. Is there a way to have brackets for the testing if the variables match? I plan to have it print that it is wrong when it is for the else, and also have it increase thee grade when it is correct/true.
danpost danpost

2015/5/7

#
biferony wrote...
I did this, but it just printed out 20 of the phrase, even though the arrays only matched around five times. Is there a way to have brackets for the testing if the variables match? I plan to have it print that it is wrong when it is for the else, and also have it increase thee grade when it is correct/true.
You need to replace the phrase with what you actually wish to have output. You can initialize an int variable to count the number of correct responses before the loop and increment it within the loop when a correct response is found:
int matchCount = 0;
for (int n=0; n<20; n++)
{
    // collect and print data (as above, adjusting what prints)
    if (match)
    {
        matchCount++;
        // output "banana"
    }
    else
    {
        // output "lemon"
    }
}
System.out.println("summary output"); // adjust what prints here also
You can use multiple print lines for any and all output information, if needed. For example:
System.out.println("QUESTION "+qNum);
System.out.println("submitted answer: "+submitted);
System.out.println("correct answer: "+actual);
// etc.
biferony biferony

2015/5/7

#
danpost wrote...
You need to replace the phrase with what you actually wish to have output. You can initialize an int variable to count the number of correct responses before the loop and increment it within the loop when a correct response is found:
Thank you so much! I wouldn't have been able to do this without you!
You need to login to post a reply.