Posts Tagged average of numbers
Scanner to read double numbers from keyboard and display their average.
It uses scanner to read double numbers from keyboard and display their average. It is assumed that input is terminated by the string “done”.
import java.util.*;
class AvgNums{
public static void main (String a[]){
Scanner conin = new Scanner (System.in);
int count =0;
double sum=0.0;
System.out.println("Enter Numbers:: ");
// Read and Sum numbers
while (conin.hasNext() ) {
if(conin.hasNextDouble () ) {
sum += conin.nextDouble();
count++;
}
else {
String str = conin.next();
if(string.equals ("done")) break;
else {
System.out.println("ERROR");
return;
}
}
}
System.out.println("Average is "+ sum/ count);
}
}


Recent Comments