Do not include the number in the calculations if it is below 0

2018-10-23 23:21:03

Step 1

Using a loop, ask the user to enter 6 numbers, then find their average.

Step 2

Also count and display the number of even numbers.

Step 3

If a value is below zero, print "Invalid Entry" and do not include the number in

the calculations. last part is the where I failed to happen any advices and helps are appreciated.

import java.util.Scanner;

public class Lab_3 {

public static void main (String []args) {

Scanner input = new Scanner(System.in);

int sum = 0;

int evenNumbers = 0;

for(int i = 0; i < 6; i++)

{

System.out.println("Please enter number");

int x = input.nextInt();

sum += x;

if ( x < 0)

sum = sum + x ;

if (x %2 ==0)

evenNumbers++;

boolean z = true;

if ( x <0)

System.out.println("Invalid entry");

}

double avg = (double) sum / 6;

System.out.println("Average is " + avg);