在方法

时间:2015-11-17 15:23:46

标签: java arrays swing

我在方法之间传递数组时遇到困难,我设法将它们全部设置为boolean的假,并将数组返回到主数据库。但是从那里我不知道如何将它传递给另一个方法,然后将boolean true数组显示为“yes”或将boolean false数组显示为“no”。我的代码如下:

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:");
    }

}

4 个答案:

答案 0 :(得分:1)

为了让您开始......您可以将arrays方法的结果设置为main方法中的局部变量,并作为参数传递给seen。然后,您可以对display方法执行相同的操作。

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

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

网上有很多关于此类内容的教程。 Here就是一个例子。

答案 1 :(得分:1)

您需要将其作为参数传递或声明全局数组。

  1. 通过参数传递:
  2. 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. 作为全球数组:
    2. 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++)
         {...}
      }
      

      }

答案 2 :(得分:1)

声明

boolean [] birds = new boolean [5];

作为您班级中所有方法的可访问对象。

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;
    }

    ...
}

答案 3 :(得分:0)

这是模仿你自己的实现:

导入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..
   }

}