Educoder - Java入門 - 數組基礎各關卡題目總結


第1關:初識數組

1、編程要求:

  在Begin-End區域中定義一個int類型數組 scores,錄入三個值,918860,最后輸出數組中的三個值,效果如圖:

 

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、測試效果:  

  我會對你編寫的代碼進行測試:

  測試輸入:49151232

  預期輸出:

  平均值:44.0

  最大值:91

  測試輸入:511511222100

  預期輸出:

  平均值:57.2

  最大值:151

 

3、程序設計思路:

 (1)此題需要尋找兩個容器,一個是所有數據的和,一個是最大值。

 (2)注意平均值運用時需要強制轉換。

 (3)手動輸入需要引入Scanner類。

 

4、程序實現:

  1. package step3;
  2. import java.util.Scanner;
  3. public class HelloWorld {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int[] scores = new int[sc.nextInt()];
  7. //循環給數組賦值
  8. for(int i = 0 ; i< scores.length;i++){
  9. scores[i] = sc.nextInt();
  10. }
  11. /********** Begin **********/
  12. //在這里計算數組scores的平均值和最大值
  13. int sum = 0;
  14. int max = scores[0];
  15. for (int i = 0; i < scores.length; i++) {
  16. sum += scores[i];
  17. if(max < scores[i]){ //求最大值
  18. max = scores[i];
  19. }
  20. }
  21. System.out.println("平均值:" + (double)sum/scores.length);
  22. System.out.println("最大值:" + max);
  23. /********** End **********/
  24. }
  25. }

 

 


第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  

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM