如果我有一个在运行程序时更新的数组(假设我向玩家要了5个数字),调用is ClassA,然后想用不同的类保存它,调用从ClassA导入数据的ClassB,我该怎么做呢?
我可以创建一个全局变量并将该信息传递给ClassB,但是如何将一个接受不同方法的参数的方法传递给这个文件呢?
以下是我到目前为止所尝试的:
class quiz
{
--- other code irrelavent to question ---
public static int[] scorearrayp1()
{
int[] scorep1 = new int[4]; // this creates the array that gets filled later
return new int[4];
}
public static void askQ(scorep1)
{
JOptionPane.showInputDialog("Some numbers");
// this method then fills the array depending on user input
// it is the values from here that I wish to save in classB
}
public static int[] passArray(int[] scorep1)
{
return scorep1; // this is from class A and takes the value from a method before
}这是我想要将这个数组发送到的类:
class saveScores
{
public static void main(String[] params) throws IOException
{
PrintWriter outputStream = new PrintWriter(new FileWriter("scores.txt"));
finalproject data = new finalproject();
int[] scores = data.passArray(int[] scorep1);
for (int i = 0; i < scores.length; i++)
{
outputStream.println(scores[i]);
}
outputStream.close();
System.exit(0);
}
} 现在我被抛出了两个错误
error:'.class' expected
int[] scores = data.passArray(int[] scorep1);
^
error:';' expected
int[] scores = data.passArray(int[] scorep1);
^我本想将passArray(int[] scorep1)更改为passArray(scorep1),但它只是告诉我找不到该符号。
发布于 2015-12-08 16:16:24
变化
int[] scores = data.passArray(int[] scorep1);至
int[] scores = data.passArray(scorep1);但是您仍然需要声明scorep1,例如:
int[] scorep1 = new int[]{1, 0, 0}根据您编辑的问题(如果我理解正确的话),您的代码应该如下所示:
int[] scores = quiz.passArray(quiz.scorearrayp1());无论如何,请记住类名应该总是大写的。
发布于 2015-12-08 16:16:56
你可以这样做:
class quiz{
int[] scorep1 = null;
public quiz()
{
scorep1 = new int[4]; // this creates the array that gets filled later
}
public static int[] askQ()
{
JOptionPane.showInputDialog("Some numbers");
// here call fills the array depending on user input
}然后这样做:
int[] somearray = quiz.askQ();
int[] scores = data.passArray(somearray);您应该创建一个类似于上面的数组somearray来传递给data.passArray(somearray);方法
https://stackoverflow.com/questions/34150932
复制相似问题