Advertisement
Uimin_Maxim

Задача 7

Jan 27th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. //Уймин Максим
  2. //IT School, 2 группа
  3. //09.10.2015
  4. //Задача 7. Подсчитать количество положительных и отрицательных чисел в массиве
  5. import java.io.PrintStream;
  6. import java.util.Scanner;
  7. public class Exersise_7 {
  8.     public static PrintStream out = System.out;
  9.     public static Scanner scan = new Scanner(System.in);
  10.     public static int[] CreateArray (int N){
  11.         //Функция создаёт массив и заполняет его случайными числами
  12.         int[]array = new int[N];
  13.         out.println("Original array:");
  14.         for (int i=0;i<N;i++){
  15.             array[i]=(-10) + (int)(Math.random()*((10-(-10))+1));
  16.             out.print(array[i]+" ");
  17.         }
  18.         out.println();
  19.         return array;
  20.     }
  21.     public static void CountElements (int[] array,int N){
  22.         //Функция считает количество положительных и отрицательных чисел
  23.         int even = 0;
  24.         int odd = 0;
  25.         for (int i=0;i<N;i++){
  26.             if (array[i]%2==0) even++;
  27.             if (array[i]%2!=0) odd++;
  28.         }
  29.         out.println("There are "+even+" even numbers and there are "+odd+" odd numbers");
  30.     }
  31.     public static void MainFunction (){
  32.         //Здесь собран весь функционал программы
  33.         out.println("Enter the array size(intger number, greather then zero)");
  34.         int N = scan.nextInt();
  35.         int[] array=new int [N];
  36.         array = CreateArray(N);
  37.         CountElements(array, N);
  38.     }
  39.     public static void main (String [] args){
  40.         //Основная функция, отсюда начинается выполнение приложения
  41.         MainFunction();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement