Count Method
題目說明
請寫出Count方法,計算陣列中目標數出現的次數。
請完成註解TODO部分,使程式符合題目要求。
⚠️ 請依照程式模板進行撰寫,若人工檢查時發現結構變更將無法得分。
import java.util.Scanner;
public class defindCountMethod {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] nums = new int[10];//陣列長度10
for (int i = 0; i < 10; i++) {//存入10個數值於陣列中
nums[i] = input.nextInt();
}
int target = input.nextInt();//目標數值
System.out.print(target + " has " + Count(nums, target)); // 呼叫副程式:Count方法
}
// TODO:請實作Count方法(確認目標數值出現的次數)
}
輸入
陣列資料:10個正整數
目標數:任一正整數
輸出
判斷10個數值中目標數出現的次數。並輸出:
(目標數) has (幾個)
範例輸入 #1
2 5 3 1 0 4 1 7 12 33
0
範例輸出 #1
0 has 1
說明:0在陣列裡出現1次
範例輸入 #2
2 1 2 1 2 1 2 1 1 1
1
範例輸出 #2
1 has 6
說明:1在陣列裡出現6次
Comments