我想要實現的是:用掃描儀輸入數組,現在不能打印數組?
- 創建一個數組,並定義其與用戶輸入(掃描儀)的長度。 ✓
- 循環遍歷數組以填充其值。 ✓
- 定義其長度並根據長度填充值後,打印整個陣列。 X
我無法達到第3位。有人可以幫我嗎?我只需要打印數組。
這是我的代碼:
package arrayinputoutput;
import java.util.Scanner;
public class ArrayInputOutput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x;
int[] test;
System.out.println("How long should the array be?");
x = input.nextInt();
for (int i = 0; i < x + 1; i++) {
input.nextLine();
System.out.println("Please fill in position " + i + ":");
i = input.nextInt();
}
//System.out.println(test[]);
}
}
但是你沒有填滿陣列!在讀取每個值時,必須在每個索引上填充數組。 –
@RenatoAfonso這不是真正的問題;這段代碼甚至沒有編譯。 – progyammer
@progyammer首先從你認爲它不能編譯的東西來看?它只編譯FINE。 –