直接上代碼和截圖
public static void main(String[] args) {
// 定義數組的3種方式
// 第一種
String[] role = new String[3];
role[0] = "建甯公主";
role[1] = "雙兒";
role[2] = "阿珂";
System.out.println(role[0] + "," + role[1] + "," + role[2]);
// 也可以把[]放在變量名後面,如下:
String role2[] = new String[4];
role2[0] = "蘇荃";
role2[1] = "方怡";
role2[2] = "曾柔";
role2[3] = "沐劍屏";
System.out.println(role2[0] + "," + role2[1] + "," + role2[2] + "," + role2[3]);
// 第二種
int[] score = {88, 66, 89, 73};
System.out.println(score[0] + "," + score[1] + "," + score[2] + "," + score[3]);
int score2[] = {25, 39, 17, 92};
System.out.println(score2[0] + "," + score2[1] + "," + score2[2] + "," + score2[3]);
// 第三種
double[] ratings = new double[]{0.39, 0.76};
System.out.println(ratings[0] + "," + ratings[1]);
double ratings2[] = new double[]{8.76, 12.93};
System.out.println(ratings2[0] + "," + ratings2[1]);
}
運作結果如下: