We would like to check Negative, Positive and Zero Values using if
statement.
Write a program that inputs five numbers
Determine and print the number of negative numbers input, the number of positive numbers input and the number of zeros input.
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); //your code here }/*from w w w .j a v a 2 s . c o m*/ // determine value of integer private static boolean isPositive(int x){ return x > 0; } private static boolean isNegative(int x){ return x < 0; } private static boolean isZero(int x){ return x == 0; } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int positive = 0, negative = 0, zero = 0; for(int i=0; i<5; i++){ System.out.printf("%d/5. Enter integer: ", i+1); int val = sc.nextInt(); if(isPositive(val)){ positive++; }else if(isNegative(val)){ negative++; }else if(isZero(val)){ zero++; } } System.out.printf("Negative = %d\nPositive = %d\nZeros = %d\n", negative, positive, zero); } // determine value of integer private static boolean isPositive(int x){ return x > 0; } private static boolean isNegative(int x){ return x < 0; } private static boolean isZero(int x){ return x == 0; } }