以对象数组作为参数调用方法
Invoking method with array of objects as parameter
您好,我的方法 bugScan 有问题 运行 我认为这是因为我已将 myBugs 声明为对象数组。如果有人能给我任何提示或指出正确的方向来解决这个问题,我将非常感激 :) 以下是所有合适的 classes:
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
public void bugScan(aBug[] bugObjects, Scanner scan) {
String inSpecies, inName;
int inX, inY, inEnergy, inId;
for(int i = 0; i < bugObjects.length; i++)
{
System.out.print("Species: ");
inSpecies = scan.nextLine();
System.out.print("Name: ");
inName = scan.nextLine();
System.out.print("X position: ");
inX = scan.nextInt();
System.out.print("Y position: ");
inY = scan.nextInt();
System.out.print("Energy: ");
inEnergy = scan.nextInt();
System.out.print("ID: ");
inId = scan.nextInt();
bugObjects[i].symbol = '*';
bugObjects[i].species = inSpecies;
bugObjects[i].name = inName;
bugObjects[i].x = inX;
bugObjects[i].y = inY;
bugObjects[i].energy = inEnergy;
bugObjects[i].id = inId;
}
}
}
这是另一个 class:
package mainFuncs;
import java.util.Scanner;
public class mainMenu {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
aBug[] myBugs = new aBug[5];
myBugs.bugScan(myBugs, scan);
System.out.print("hi");
}
}
我的问题出现在以下行:
myBugs.bugScan(myBugs, 扫描);
错误消息 "Cannot invoke bugScan(aBug[], Scanner) on the array type aBug[]"
类型参数不接受Bug。自己写的类,名字应该是驼峰写的,这样才算例外。
问题出在你提到的那一行!!
myBugs
是一个数组,没有任何方法 bugScan
应该是这样的
new aScan().bugScan(myBugs, scan);
您在 aBug
class 中定义了方法,但您试图在 aBug
对象的数组上调用它!无论如何,我认为您可以从第一个 class 中取出 bugScam
方法并将其放在第二个 class.
中
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
}
第二个class:
package mainFuncs;
import java.util.Scanner;
public class mainMenu {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
aBug[] myBugs = new aBug[5];
bugScan(myBugs, scan);
System.out.print("hi");
}
public static void bugScan(aBug[] bugObjects, Scanner scan) {
String inSpecies, inName;
int inX, inY, inEnergy, inId;
for(int i = 0; i < bugObjects.length; i++)
{
System.out.print("Species: ");
inSpecies = scan.nextLine();
System.out.print("Name: ");
inName = scan.nextLine();
System.out.print("X position: ");
inX = scan.nextInt();
System.out.print("Y position: ");
inY = scan.nextInt();
System.out.print("Energy: ");
inEnergy = scan.nextInt();
System.out.print("ID: ");
inId = scan.nextInt();
bugObjects[i].symbol = '*';
bugObjects[i].species = inSpecies;
bugObjects[i].name = inName;
bugObjects[i].x = inX;
bugObjects[i].y = inY;
bugObjects[i].energy = inEnergy;
bugObjects[i].id = inId;
}
}
}
myBugs
是一个数组,所以你只能对它进行数组运算。您的方法 bugScan()
定义在 aBug
class 中。如果你想运行它你可以通过这样做来解决它:
Bug tmp = new aBug();
tmp.bugScan(myBugs, scan);
但这闻起来像糟糕的编码,aBug class 不应该对 'scan for bugs' 负责。
旁注:按照惯例 class 名称以大写字母开头,即使您应用修复程序,它也会抛出 NullPointerException,因为您从未初始化数组。
您好,我的方法 bugScan 有问题 运行 我认为这是因为我已将 myBugs 声明为对象数组。如果有人能给我任何提示或指出正确的方向来解决这个问题,我将非常感激 :) 以下是所有合适的 classes:
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
public void bugScan(aBug[] bugObjects, Scanner scan) {
String inSpecies, inName;
int inX, inY, inEnergy, inId;
for(int i = 0; i < bugObjects.length; i++)
{
System.out.print("Species: ");
inSpecies = scan.nextLine();
System.out.print("Name: ");
inName = scan.nextLine();
System.out.print("X position: ");
inX = scan.nextInt();
System.out.print("Y position: ");
inY = scan.nextInt();
System.out.print("Energy: ");
inEnergy = scan.nextInt();
System.out.print("ID: ");
inId = scan.nextInt();
bugObjects[i].symbol = '*';
bugObjects[i].species = inSpecies;
bugObjects[i].name = inName;
bugObjects[i].x = inX;
bugObjects[i].y = inY;
bugObjects[i].energy = inEnergy;
bugObjects[i].id = inId;
}
}
}
这是另一个 class:
package mainFuncs;
import java.util.Scanner;
public class mainMenu {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
aBug[] myBugs = new aBug[5];
myBugs.bugScan(myBugs, scan);
System.out.print("hi");
}
}
我的问题出现在以下行: myBugs.bugScan(myBugs, 扫描); 错误消息 "Cannot invoke bugScan(aBug[], Scanner) on the array type aBug[]"
类型参数不接受Bug。自己写的类,名字应该是驼峰写的,这样才算例外。
问题出在你提到的那一行!!
myBugs
是一个数组,没有任何方法 bugScan
应该是这样的
new aScan().bugScan(myBugs, scan);
您在 aBug
class 中定义了方法,但您试图在 aBug
对象的数组上调用它!无论如何,我认为您可以从第一个 class 中取出 bugScam
方法并将其放在第二个 class.
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
}
第二个class:
package mainFuncs;
import java.util.Scanner;
public class mainMenu {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
aBug[] myBugs = new aBug[5];
bugScan(myBugs, scan);
System.out.print("hi");
}
public static void bugScan(aBug[] bugObjects, Scanner scan) {
String inSpecies, inName;
int inX, inY, inEnergy, inId;
for(int i = 0; i < bugObjects.length; i++)
{
System.out.print("Species: ");
inSpecies = scan.nextLine();
System.out.print("Name: ");
inName = scan.nextLine();
System.out.print("X position: ");
inX = scan.nextInt();
System.out.print("Y position: ");
inY = scan.nextInt();
System.out.print("Energy: ");
inEnergy = scan.nextInt();
System.out.print("ID: ");
inId = scan.nextInt();
bugObjects[i].symbol = '*';
bugObjects[i].species = inSpecies;
bugObjects[i].name = inName;
bugObjects[i].x = inX;
bugObjects[i].y = inY;
bugObjects[i].energy = inEnergy;
bugObjects[i].id = inId;
}
}
}
myBugs
是一个数组,所以你只能对它进行数组运算。您的方法 bugScan()
定义在 aBug
class 中。如果你想运行它你可以通过这样做来解决它:
Bug tmp = new aBug();
tmp.bugScan(myBugs, scan);
但这闻起来像糟糕的编码,aBug class 不应该对 'scan for bugs' 负责。
旁注:按照惯例 class 名称以大写字母开头,即使您应用修复程序,它也会抛出 NullPointerException,因为您从未初始化数组。