mardi 19 mars 2019

I have some trouble with my Java code. It won't run when telling the difference between positive and negative number when in a loop

So this is my question I am trying to solve. Write a program called MyWhiley1 that asks the user to type a positive integer. When the user types a negative value the program writes ERROR and asks for another value. When the user types 0 that means that the last value has been typed and the program must write the average of the positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'. But I have some errors my code does not run properly if I type a positive number and then a negative number nothing happens.

import java.util.Scanner;
public class MyWhiley1 
{
public static void main(String[]arg)
{
    int sum = 0,list=0;
    Scanner reader=new Scanner(System.in);
    System.out.println("Type a postive integer if you enter zero the list is compelete");
    int number=reader.nextInt();
    do
    {
        if (number>0)
        {
            sum+=number;
            list++;
        }
        else if (number<0)  
        {
            System.out.println("ERROR type in a postive integer");

        }
        reader.close();
    }while (number!=0);

    if (list!=0)
    {
        System.out.println("Average is: "+sum/list);
    }
    else 
        System.out.println("NO average");
}
}

Aucun commentaire:

Enregistrer un commentaire