Need a program that reads the numbers from the keyboard until we enter 0 and determines how many positive and negative numbers have been entered

int[] a = {8, 2, 7, -2, 5, 6, 3, 1, 9, -3, -6, -8, -1, -4, -5, -7, -9}; System.out.println("Массив целых чисел:\n"); System.out.println(Arrays.toString(a)); System.out.println("\nОтрицательные числа:"); for (int value : a) { if (value < 0) { System.out.println(value); } } System.out.println("\nПоложительные числа числа:"); for (int value : a) { if (value >= 0) { System.out.println(value); } } System.out.println("\nСортировка по возрастанию:\n"); Arrays.sort(a); System.out.println(Arrays.toString(a)); 
  • 2
    Well, write it down, since you need it, and if it doesn't work out - write here, help - Ep1demic
  • the fact is that I can't do it, I was looking for all sorts of methods, but not that - Sasha
  • one
    press the edit button and add the code that you have already written before ... - Ep1demic
  • This is what I tried, but without do, while, and yes, the result should be different. I need to display positive numbers: n number and negative: n number - Sasha

2 answers 2

You need to add a counter variable and exit from the loop when it is zero, something like this:

 int[] a = {8, 2, 7, -2, 5, 6, 3, 1, 9, -3, -6, -8, -1, -4, -5, -7, -9}; System.out.println("Массив целых чисел:\n"); System.out.println(Arrays.toString(a)); int countNegative = 0; int countPositive = 0; for (int value : a) { if (value < 0) { countNegative++; } else if(value > 0) { countPositive++; } else { break; } } System.out.println("\nОтрицательные числа:" + countNegative); System.out.println("\Положительные числа:" + countPositive); System.out.println("\nСортировка по возрастанию:\n"); Arrays.sort(a); System.out.println(Arrays.toString(a)); 
      Scanner systemInScanner = new Scanner(System.in); int number = -1; int pozCount = 0; //количество положительных чисел int negCount = 0; //количество отрицательных чисел while(number != 0){ number = systemInScanner.nextInt(); if(number > 0) pozCount++; else if (number < 0) negCount++; }