Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Asks for Integers till a negative is entered

$
0
0
Im trying to get this while loop to work. The program keeps asking for integers till a negative is entered then it prints out the average

I think the problem is in the while loop but im not sure. Any help?


public static void main(String[] args) {

int count = 0;
int sum = 0;
boolean finished = false;

while(!finished);{
Scanner myInput = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = myInput.nextInt();


if (n < 0)

   finished = true;
else
   sum = (sum + n);
   count = count+1;

System.out.println(sum/count);

}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>