第1關:初識數組
1、編程要求:
在Begin-End
區域中定義一個int
類型數組 scores
,錄入三個值,91
,88
,60
,最后輸出數組中的三個值,效果如圖:
2、測試效果:
3、程序設計思路:
(1)利用數組的省略靜態創建法快速創建數組,再按照索引值對應輸出數組中的元素值。
4、程序實現:
package step1; public class HelloWorld { public static void main(String[] args) { /********** Begin **********/ int[] scores = {91,88,60}; System.out.println("數組的第一個值為:"+scores[0] ); //在這里輸出數組的第一個值 System.out.println("數組的第二個值為:"+scores[1] ); //在這里輸出數組的第二個值 System.out.println("數組的第三個值為:"+scores[2] ); //在這里輸出數組的第三個值 /********** End **********/ } }
第2關:數組的使用
1、編程要求:
使用本關所學知識,根據右側編輯器Begin-End
中提示補充代碼,賦值並輸出數組中的信息。
2、測試效果:
我會對你的程序測試兩次:
輸入:3
預期輸出:
數組中的第一個數據為:張三
數組中的第二個數據為:張無忌
數組中的第三個數據為:張三豐
數組中的第四個數據為:張歲山
數組scores的長度為:3
輸入:5
預期輸出:
數組中的第一個數據為:張三
數組中的第二個數據為:張無忌
數組中的第三個數據為:張三豐
數組中的第四個數據為:張歲山
數組scores的長度為:5
3、程序設計思路:
(1)靈活運用數組的靜態初始化和動態初始化創建數組並運用索引值輸出。
(2)由於要手動輸入,需要引入Scanner類。
4、程序實現:
package step2; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { /*******begin*******/ //在這里定義一個長度為 4 的字符串數組,用來存放學生姓名 String[] stuNames = new String[4] ; //在這里給stuNames數組賦值 分別為 張三,張無忌,張三豐,張歲山 stuNames[0] = "張三"; stuNames[1] = "張無忌"; stuNames[2] = "張三豐"; stuNames[3] = "張歲山"; //在這里輸出stuNames數組中的數據 System.out.println("數組中的第一個數據為:" + stuNames[0]); System.out.println("數組中的第二個數據為:" + stuNames[1]); System.out.println("數組中的第三個數據為:" + stuNames[2]); System.out.println("數組中的第四個數據為:" + stuNames[3]); int[] scores; Scanner sc = new Scanner(System.in); //在這里獲取系統輸入的整數來初始化scores數組的長度 int length = sc.nextInt() ; scores = new int[length]; /*******end*******/ System.out.println("數組scores的長度為:" + scores.length); } }
第3關:選擇題
1、選擇題答案:C D ABCD
第4關:數組練習-平均值和最大值
1、編程要求:
根據提示,在右側編輯器Begin-End
處補充代碼,計算並輸出數組的平均值和最大值。
2、測試效果:
我會對你編寫的代碼進行測試:
測試輸入:4
,91
,51
,2
,32
;
預期輸出:
平均值:44.0
最大值:91
測試輸入:5
,1
,151
,12
,22
,100
;
預期輸出:
平均值:57.2
最大值:151
3、程序設計思路:
(1)此題需要尋找兩個容器,一個是所有數據的和,一個是最大值。
(2)注意平均值運用時需要強制轉換。
(3)手動輸入需要引入Scanner類。
4、程序實現:
package step3;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] scores = new int[sc.nextInt()];
//循環給數組賦值
for(int i = 0 ; i< scores.length;i++){
scores[i] = sc.nextInt();
}
/********** Begin **********/
//在這里計算數組scores的平均值和最大值
int sum = 0;
int max = scores[0];
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
if(max < scores[i]){ //求最大值
max = scores[i];
}
}
System.out.println("平均值:" + (double)sum/scores.length);
System.out.println("最大值:" + max);
/********** End **********/
}
}
第5關:二維數組
1、編程要求:
(1)在右側Begin-End
區域中定義如下二維數組,使用for
循環輸出數組中所有的數據:
(2)
使用for
循環將上述數組中的數據全部改為:
2、測試效果:
(1)
(2)
3、程序設計思路:
(1)按照要求先創建數組,然后采用嵌套循環循環輸出數組中的元素即可。
4、程序實現:
kage step4; public class HelloWorld { public static void main(String[] args) { /********** Begin **********/ int[][] scores = { {92,85}, {91,65}, {90,33} }; for (int i = 0; i < scores.length; i++) { for (int j = 0; j < scores[i].length; j++) { System.out.println(scores[i][j]); scores[i][j] = j+1; } } for (int i = 0; i < scores.length; i++) { for (int j = 0; j < scores[i].length; j++) { System.out.println(scores[i][j]); } } /********** End **********/ } }
第6關:選擇題
1、選擇題答案:C B