Java:在对象类内部创建数组?
我试图在对象类中保存Java:在对象类内部创建数组?,java,arrays,object,for-loop,Java,Arrays,Object,For Loop,我试图在对象类中保存x数量的整数。我正在通过数组进行尝试,但不确定这是否可行,到目前为止,eclipse给了我两个错误。一个要求我在我的Gerbil()类中插入赋值运算符,另一个要求我不能对非静态字段food进行static引用。我要寻找的结果是food 1=第一次输入;食物2=第二次输入直到它达到食物总量 以下是我目前的代码: import java.util.Scanner; public class Gerbil { public String name; public String i
x
数量的整数。我正在通过数组进行尝试,但不确定这是否可行,到目前为止,eclipse给了我两个错误。一个要求我在我的Gerbil()
类中插入赋值运算符,另一个要求我不能对非静态字段food
进行static
引用。我要寻找的结果是food 1=第一次输入;食物2=第二次输入代码>直到它达到食物总量
以下是我目前的代码:
import java.util.Scanner;
public class Gerbil {
public String name;
public String id;
public String bite;
public String escape;
public int[] food;
public Gerbil() {
this.name = "";
this.id = "";
this.bite = "";
this.escape = "";
this.food[]; // I'm not sure what I should put here. This is where I want to store
} // the different integers I get from the for loop based on the
// total number of foods entered. So if totalFoods is 3, there should
// be 3 integers saved inside of the object class based on what's typed
// inside of the for-loop. Or if totalFoods = 5, then 5 integers.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many foods?");
int totalFood = keyboard.nextInt();
System.out.println("How many gerbils in the lab?");
int numberOfGerbils = keyboard.nextInt();
Gerbil[] GerbilArray = new Gerbil[numberOfGerbils];
for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil();
System.out.print("Lab ID:");
String id = keyboard.next();
System.out.print("Gerbil Nickname:");
String name = keyboard.next();
System.out.print("Bite?");
String bite = keyboard.next();
System.out.print("Escapes?");
String city = keyboard.nextLine();
for (int j = 0; j < totalFood; j++) {
System.out.println("How many of food " + (j+1) + "do you eat?:");
food[j] = keyboard.nextInt();
}
}
}
}
import java.util.Scanner;
公营沙鼠{
公共字符串名称;
公共字符串id;
公共绳索咬伤;
公共字符串逃逸;
公共食品;
公沙鼠(){
this.name=“”;
this.id=“”;
这是“咬”;
此参数为:escape=“”;
这个。食物[];//我不确定我应该放什么。这是我想储存的地方
}//根据
//输入的食物总数。因此,如果totalFoods为3,则
//根据类型,在对象类中保存3个整数
//在for循环内部。或者如果totalFoods=5,则为5个整数。
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“有多少食物?”);
int totalFood=keyboard.nextInt();
System.out.println(“实验室里有多少沙鼠?”);
int numberOfGerbils=keyboard.nextInt();
沙鼠[]沙鼠群=新沙鼠[沙鼠数量];
对于(int i=0;i您需要传递沙鼠构造函数中的食物数量:
public Gerbil(int totalFood) {
this.name = "";
this.id = "";
this.bite = "";
this.escape = "";
this.food[] = new int[totalFood];
}
然后在循环中,将如下所示:
for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil(totalOfFood);
System.out.print("Lab ID:");
String id = keyboard.next();
System.out.print("Gerbil Nickname:");
String name = keyboard.next();
System.out.print("Bite?");
String bite = keyboard.next();
System.out.print("Escapes?");
String city = keyboard.nextLine();
for (int j = 0; j < totalFood; j++) {
System.out.println("How many of food " + (j+1) + "do you eat?:");
GerbilArray[i].food[j] = keyboard.nextInt();
}
for(int i=0;ifood[j]=keyboard.nextInt();
。这是谁的食物?food[j]是沙鼠[i]的食物。我希望它运行j=食物总数。但我希望它能够保存每种特定食物,并对每种沙鼠执行此操作。因此沙鼠[i+1]将被要求相同的食物,并且可以为每种食物[j]保存不同的数字.这是因为我只是因为你找到了答案,并不意味着你应该澄清问题并告诉我们必须删除它。然后你应该投票支持我的答案。这就是这个网站的全部概念。