Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2012 20:01
Show Gist options
  • Select an option

  • Save anonymous/1952702 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1952702 to your computer and use it in GitHub Desktop.
import java.util.*;
public class AddFloats{
public static void main(String[] args){
System.out.println("Please enter a positive floating point decimal to continue, or a negative number to halt");
Scanner standardIn = new Scanner(System.in);
float sum = 0.0;
float next_number = standardIn.nextFloat();
float average = 0.0;
int numberOfEntries = 0;
while(standardIn.hasNextFloat()){
if(next_number >=0 ){
numberOfEntries++;
sum += next_number;
}else{ break;}
System.out.println("Please enter a positive floating point decimal to continue, or a negative number to halt");
}
System.out.println("The average of the values you entered is: " + (float)Math.round((sum/numberOfEntries) * 10) / 10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment