// CSE 142, Autumn 2007, Marty Stepp // This program reads ten integers from the user and displays counts and // sums of the negative and non-negative numbers. // example input: 2 1 -4 7 -19 3 5 -8 -1 6 import java.util.*; // for Scanner public class Sums { public static void main(String[] args) { Scanner console = new Scanner(System.in); // cumulative sums and counts of all negative and non-negative numbers int countNeg = 0; int countNonNeg = 0; int sumNeg = 0; int sumNonNeg = 0; System.out.print("Type ten numbers: "); for (int i = 1; i <= 10; i++) { int num = console.nextInt(); if (num < 0) { // negative countNeg++; sumNeg = sumNeg + num; } else { // non-negative countNonNeg++; sumNonNeg = sumNonNeg + num; } } // print results System.out.println(countNeg + " negative, " + countNonNeg + " non-negative"); System.out.println("negative sum " + sumNeg + ", non-negative sum " + sumNonNeg); } }