您好我在运行我的方法bugScan时遇到问题,我认为这是因为我已将myBugs声明为对象数组。如果任何可以给我任何提示或指出我正确的排序方向我会非常感谢:)以下是所有适当的类:
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;
}
}
}
这是另一个类:
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,scan); 错误消息&#34;无法在数组类型aBug []&#34;
上调用bugScan(aBug [],Scanner)答案 0 :(得分:0)
一个Bug的类型参数是不可接受的。通过自己编写的类,名称应该用camel case编写,因此可能是异常的情况。
答案 1 :(得分:0)
问题在于你提到的那条线!!
myBugs
是一个数组,没有任何方法bugScan
它应该是这样的
new aScan().bugScan(myBugs, scan);
答案 2 :(得分:0)
您在aBug
类中定义了该方法,但是您尝试在aBug
个对象的数组上调用它!无论如何,我认为您可以从第一个类中取出bugScam
方法并将其放在第二个类中。
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
}
第二课:
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;
}
}
}
答案 3 :(得分:0)
myBugs
是一个数组,因此您只能对其执行数组操作。您的方法bugScan()
在aBug
类中定义。如果你想运行它,你可以通过这样做来解决它:
Bug tmp = new aBug();
tmp.bugScan(myBugs, scan);
但这有点像编码错误,aBug类不应该负责扫描错误&#39;。
旁注:通过对流类名称以大写开头,即使你应用了修复,它也会抛出一个NullPointerException,因为你永远不会初始化你的数组。