0

How these works i dont understands ?!!!

import java.util.Scanner; public class Vector { public static void main (String[] args) { Scanner input = new Scanner(System.in); int L; int[] vector; int P = 0; int N = 0; int Z = 0; do { System.out.print("Enter the length of the vector: "); L = input.nextInt(); } while ( L <= 0 ); vector = new int[L]; for (int i=0; i <=L-1; i++) { System.out.print("Enter vector[" +i+ "]: " ); vector[i] = input.nextInt(); if ( vector[i] > 0 ) { P = P + 1; } else if ( vector[i] < 0 ) { N = N + 1; } else { Z = Z + 1; } } System.out.print("The number of positive numbers is: " +P+ "\n" ); System.out.print("The number of negative numbers is: " +N+ "\n" ); System.out.pri

25th May 2020, 10:02 PM
Hozayfa Osama
Hozayfa Osama - avatar
4 Answers
+ 1
The do while loop just makes sure that you enter a positive value. All you are doing there is getting the size of the array. You then use the for loop to populate the array with values and at each iteration you are checking whether the value entered is- 1) greater that 0 2) less than 0 3) or equal to 0 P and N hold the count of positive and negative values in the array.
25th May 2020, 10:41 PM
Avinesh
Avinesh - avatar
0
How P and N holds postive and negative
25th May 2020, 10:48 PM
Hozayfa Osama
Hozayfa Osama - avatar
0
Read carefully, I said they hold the 'count' of positive and negative values. When we say P = P+1 here P is incremented each time the value entered is greater than 0. Similarly for other cases.
25th May 2020, 10:52 PM
Avinesh
Avinesh - avatar
0
I understood thank you
25th May 2020, 10:53 PM
Hozayfa Osama
Hozayfa Osama - avatar