Advertisement
sergAccount

Untitled

Jul 3rd, 2021
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.ex16;
  7.  
  8. public class Main3 {
  9.  
  10.     /*
  11.     Задача 2
  12. Определить метод, который в качестве параметра принимает массив элементов
  13. типа boolean
  14. Метод должен вернуть (вычислить) количество элементов с значением = true.
  15. Метод должен возвращать значение типа int.
  16.  
  17. Проверить данный метод вызвать его в методе main и вывести полученный результат
  18. на экран  
  19.      */
  20.     public static int numberOfTrue(boolean[] arr) {
  21.         int result = 0;
  22.         for (int i = 0; i < arr.length; i++) {
  23.             if (arr[i]) {
  24.                 result++;
  25.             }
  26.         }
  27.         return result;
  28.     }
  29.  
  30.     public static void main(String[] args) {
  31.         boolean[] arr = {true, false, true};
  32.         int result = numberOfTrue(arr);
  33.         System.out.println("result=" + result);
  34.     }
  35.  
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement