[英]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.我在方法之间传递数组时遇到困难,我设法将它们从
boolean
全部设置为 false,并将数组返回到 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".但是从那里我不知道如何将它传递给另一个方法,然后将
boolean
真数组显示为“是”或将boolean
假数组显示为“否”。 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
.为了让您入门……您可以将
arrays
方法的结果设置为main
方法中的局部变量,并将其作为参数传递给seen
。 Then you can do the same for the display
method.然后你可以对
display
方法做同样的事情。
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.您需要将其作为参数传递或声明一个全局数组。
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++)
{...}
}
} }
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;导入 javax.swing.JOptionPane;
public class Example { private static boolean [] birds = new boolean [5];公共类示例 { 私有静态布尔 [] 鸟 = 新布尔 [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..
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.