简体   繁体   中英

Passing arrays between methods

I'm having difficulty passing arrays between methods, I've managed to set them all to false from boolean , and returned the array to the main. However from there I don't know how to pass it to another method, and then later display the boolean true array as "yes" or the boolean false array as "no". My code looks as follows:

import javax.swing.*;
class methodarrays
{
    public static void main (String[]param)
    {
        arrays();
        seen(); 
        display();
    }

    public static boolean[] arrays()
    {
        boolean [] birds = new boolean [5];
        for (int i=0;i<birds.length;i++)
        {
            birds[i]=false;
        }
        return birds;
    }
    public static boolean seen()
    {
        String quit = "100";
        String ans = "";
        while(!ans.eqauls(quit))
        {
            ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n   1) Blue Tit   2) Blackbird   3)Robin   4)Wren   5)Greenfinch");
            if (ans.equals("1"))
            {
                birds[0] = true;
                return birds[0];
            }
            else if (ans.equals("2"))
            {   birds[1] = true;
                return birds[1];
            }
            else if (ans.equals("3"))
            {
                birds[2] = true;
                return birds[2];
            }
            else if (ans.equals("3"))
            {
                birds[2] = true;
                return birds[2];
            }
            else if (ans.equals("4"))
            {
                birds[3] = true;
                return birds[3];
            }
            else if (ans.equals("5"))
            {
                birds[4] = true;
                return birds[4];
            }
        }
    }

    public static void display()
    {
        JOptionPane.showMessageDialog(null,"Your Garden Watch results are:");
    }

}

To give you are starting hand... you can set the result of your arrays method to a local variable in the main method and pass as a argument to the seen . Then you can do the same for the display method.

    public static void main (String[]param)
    {   
        boolean[] birds = arrays();
        seen(birds); 
        display(birds);
    }

    public static boolean[] arrays()
    {
    ...
    }
    public static boolean seen(boolean[] birds)
    {
    ...

There are plenty of tutorials around the web for this kind thing. Here being one example.

You need to pass it as a parameter or declare a global array.

  1. Passing by parameter:

class methodarrays {

public static void main (String[]param)
{
    boolean [] myArray =arrays();
    seen(myArray); 
    display(myArray);
}

public static boolean seen(boolean [] myArrayParam)
{
   for (int i=0;i<myArrayParam.length;i++)
   {...}
}

public static boolean display(boolean [] myArrayParam)
{
   for (int i=0;i<myArrayParam.length;i++)
   {...}
}

}

  1. As global array:

class methodarrays {

   boolean [] myArray

public static void main (String[]param)
{
    myArray = arrays();
    seen(); 
    display();
}

public static boolean seen()
{
   for (int i=0;i<myArray.length;i++)
   {...}
}

public static boolean display()
{
   for (int i=0;i<myArray.length;i++)
   {...}
}

}

Declare

boolean [] birds = new boolean [5];

as accessible object for all methods within your class.

import javax.swing.*;

class methodarrays
{   

    private boolean [] birds = new boolean [5]

    ...

    public static boolean[] arrays()
    {
        for (int i=0;i<birds.length;i++)
        {birds[i]=false;
        }
        return birds;
    }

    ...
}

Here is the implementation mimicing your own:

import javax.swing.JOptionPane;

public class Example { private static boolean [] birds = new boolean [5];

   public static void main (String[]param){ 
       arrays();
       seen(); 
       display();
   }

   public static boolean[] arrays()
   {   
       // Completely unnecessary since values are set to false by default;
       for (int i=0;i<birds.length;i++)
       {birds[i]=false;
       }
       return birds;
   }
   public static void seen(){   
       String quit = "100";
       String ans = "";
       while(!ans.equals(quit))
       {
           ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n   1) Blue Tit   2) Blackbird   3)Robin   4)Wren   5)Greenfinch");
           if (ans.equals("1"))
           {   birds[0] = true;
           }
           else if (ans.equals("2"))
           {   birds[1] = true;
           }
           else if (ans.equals("3"))
           {   birds[2] = true;
           }
           else if (ans.equals("3"))
           {   birds[2] = true;
           }
           else if (ans.equals("4"))
           {   birds[3] = true;
           }
           else if (ans.equals("5"))
           {   birds[4] = true;
           }
       }
   }

   public static void display(){
       System.out.println("Your results are: ");
       System.out.println("Blue Tit: " + birds[0]);
       System.out.println("Blackbird: " + birds[1]);
       //and so on..
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM