Java 班级获胜';无法识别用户输入?
程序将无法识别我为gerbil.foodTypes输入的值(为gerbil.foodName.length输入的值为0。为什么Java 班级获胜';无法识别用户输入?,java,arrays,class,input,user-input,Java,Arrays,Class,Input,User Input,程序将无法识别我为gerbil.foodTypes输入的值(为gerbil.foodName.length输入的值为0。为什么 class Gerbil { public int foodTypes; public String[] foodName = new String[foodTypes]; } public class mainmethod { public static void main(String args[]) { Scanner keyboard = new Sca
class Gerbil {
public int foodTypes;
public String[] foodName = new String[foodTypes];
}
public class mainmethod {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
Gerbil gerbil = new Gerbil();
System.out.println("How many types of food do the gerbils eat?");
gerbil.foodTypes = keyboard.nextInt();
for (int x = 0; x < gerbil.foodTypes ; x++) {
System.out.println("Enter the name of food item " + (x+1));
gerbil.foodName[x] = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Maximum consumed per gerbil:");
gerbil.foodMax[x] = keyboard.nextInt();
}
类沙鼠{
公共int食物类型;
公共字符串[]foodName=新字符串[foodTypes];
}
公共类方法{
公共静态void main(字符串参数[]){
扫描仪键盘=新扫描仪(System.in);
沙鼠沙鼠=新沙鼠();
沙鼠吃多少种食物;
gerbil.foodTypes=keyboard.nextInt();
对于(int x=0;x
当计算新字符串[foodTypes]时,foodTypes
为0,因此foodName
始终为零长度数组
可能会添加一个采用foodTypes
的构造函数
Gerbil gerbil = new Gerbil(keyboard.nextInt());
构造类时,数组“foodName”以零长度启动。原因是当您声明一个整数时,例如
public int foodTypes'
,编译器看到的是public int foodTypes=0;
数组初始化后无法调整大小,因此无法“重置”数组的大小
我将添加一个构造函数,该构造函数从输入中获取一个数字并将其分配给foodTypes,因此它不是0
以下是一些未经测试的代码:
class Gerbil {
public int foodTypes;
public String[] foodName;
public Gerbil(int num){
this.foodTypes = num;
this.foodName = new String[foodTypes];
}
}
public class Assignment4 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many types of food do the gerbils eat?");
Gerbil gerbil = new Gerbil(keyboard.nextInt());
for (int x = 0; x < gerbil.foodTypes ; x++) {
System.out.println("Enter the name of food item " + (x+1));
gerbil.foodName[x] = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Maximum consumed per gerbil:");
gerbil.foodMax[x] = keyboard.nextInt();
}
类沙鼠{
公共int食物类型;
公共字符串[]foodName;
公共沙鼠(国际数量){
this.foodTypes=num;
this.foodName=新字符串[foodTypes];
}
}
公共课堂作业4{
公共静态void main(字符串参数[]){
扫描仪键盘=新扫描仪(System.in);
沙鼠吃多少种食物;
沙鼠沙鼠=新沙鼠(keyboard.nextInt());
对于(int x=0;x
是关于数组的进一步文档。构造函数是做什么的?我尝试了你所说的,但没有成功。
class Gerbil {
public int foodTypes;
public String[] foodName;
public Gerbil(int num){
this.foodTypes = num;
this.foodName = new String[foodTypes];
}
}
public class Assignment4 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many types of food do the gerbils eat?");
Gerbil gerbil = new Gerbil(keyboard.nextInt());
for (int x = 0; x < gerbil.foodTypes ; x++) {
System.out.println("Enter the name of food item " + (x+1));
gerbil.foodName[x] = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Maximum consumed per gerbil:");
gerbil.foodMax[x] = keyboard.nextInt();
}