【发布时间】:2021-12-17 00:51:38
【问题描述】:
在此我试图让salutations() 方法输出在initialize() 方法中创建的数组。
我收到的错误只是告诉我当我需要它在另一个方法中时为数组创建一个局部变量。
public void initialize() {
String[] salutations = new String[]{"greetings", "hello", "good afternoon"};
String[] verses = new String[]{"we hope you are having a good Christmas", "we wish you a merry x-mas", "we wish you a good new year"};
String[] closing = new String[]{"", "b", "c"};
}
public void salutations(){
int i=1;
String x;
x=(String)Array.get(salutations, i);
System.out.println(+x+" ");
}
【问题讨论】:
-
将其存储在类的字段中,或将其作为参数传递给您的方法
-
了解范围