求出一組數字中能被b 整除的個數

題目:輸入一組數N和數字b ,求出該組數字中能被b 整除的個數。
示例:
輸入
1 2 3 4 5 6
2
輸出
3

public class ExactDivision {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Please input a array nums:");
		String str = scan.nextLine();  //nextLine會獲取到帶有空格的字符串,並把回車符吃掉,而next會自動把字符串中的空格過濾掉
		System.out.println("Please input a division again:");
		int div = scan.nextInt();
		String[] strs = str.split(" ");
		int result = 0;
		for (int i = 0; i < strs.length; i++) {
			if(Integer.valueOf(strs[i]) % div == 0){
				result++;
			}
		}
		System.out.println("這組數中能被2整除的個數:"+ result);
		scan.close();
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章