public static void main(String[] args) {
int LENGTH=0;
int currentSize=0;
String[] albumArray = new String[LENGTH];
Scanner sc = new Scanner(System.in);
System.out.println("How many tracks are in your album?");
LENGTH=sc.nextInt();
System.out.println("Thanks.");
System.out.println("Please enter " + LENGTH + " track names to add to the album: ");
//Prompts user to enter values until the array is filled. Repeats until filled.
while (currentSize < LENGTH){
System.out.print("Enter track name "+(currentSize+1)+":\t");
albumArray[currentSize] = sc.nextLine();
currentSize++;
}
for (int i =0; i<LENGTH;i++){
System.out.println(albumArray[i]);
System.out.println();
}
}
}
好的,因此基本上该程序允许用户创建相册。用户设置数组中的磁道数(LENGTH),然后将String值分配给数组中的每个索引。
在第18行下出现错误
"albumArray[currentSize] = sc.nextLine();"
有人知道我在做什么错吗?提前致谢。
最佳答案
int LENGTH=0;
int currentSize=0;
String[] albumArray = new String[LENGTH]; //can't do this yet, LENGTH is still 0
Scanner sc = new Scanner(System.in);
System.out.println("How many tracks are in your album?");
LENGTH=sc.nextInt();
知道长度后,您需要创建数组。
int LENGTH=0;
int currentSize=0;
Scanner sc = new Scanner(System.in);
System.out.println("How many tracks are in your album?");
LENGTH=sc.nextInt();
String[] albumArray = new String[LENGTH];