import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class ScoreStats
{
    public static void main(String[] args)
    {
        ArrayList<Integer> scores = getTestScores();
        
        DecimalFormat formatter = new DecimalFormat("#.##");
        System.out.println();
        System.out.println("Mean   : " + formatter.format(getMean(scores)));
        System.out.println("Std Dev: " + formatter.format(getStandardDeviation(scores)));
        
//        System.out.println(getTestScores());
        
//        ArrayList<Integer> scores = new ArrayList<Integer>();
//        scores.add(50);
//        scores.add(75);
//        scores.add(100);
//        
//        System.out.println(getMean(scores)); // 75.0
//        System.out.println(getStandardDeviation(scores)); // 20.412
    }
    
    public static ArrayList<Integer> getTestScores()
    {
        final int MIN_SCORE = 0;
        final int MAX_SCORE = 100;
        final int INVALID_VALUE = -1;
        
        Scanner fromKeyboard = new Scanner(System.in);
        
        System.out.print("Score (" + MIN_SCORE + "-" + MAX_SCORE + "): ");
        int score = asInt(fromKeyboard.nextLine(), INVALID_VALUE);
        
        while(score < MIN_SCORE || score > MAX_SCORE)
        {
            System.out.println("Invalid score");
            System.out.print("\nScore (" + MIN_SCORE + "-" + MAX_SCORE + "): ");
            score = asInt(fromKeyboard.nextLine(), INVALID_VALUE);
        }
        
        ArrayList<Integer> scores = new ArrayList<Integer>();
        
        while(score >= MIN_SCORE && score <= MAX_SCORE)
        {
            scores.add(score);
            
            System.out.print("Score (" + MIN_SCORE + "-" + MAX_SCORE + " or -1 when done): ");
            score = asInt(fromKeyboard.nextLine(), INVALID_VALUE);
        }
        
        fromKeyboard.close();
        
        return scores;
    }
    
    public static double getMean(ArrayList<Integer> scores)
    {
        int sum = 0;
        
        for(int score : scores)
            sum += score;
        
        return sum / (double) scores.size();
    }
    
    public static double getStandardDeviation(ArrayList<Integer> scores)
    {
        final double mean = getMean(scores);
        
        double sumOfSquares = 0;
        
        for(int score : scores)
            sumOfSquares += Math.pow(score - mean, 2);
        
        return Math.sqrt(sumOfSquares / scores.size());
    }
    
    public static int asInt(String str, int valueIfNotInt)
    {
        try
        {
            return Integer.parseInt(str);
        }
        catch(NumberFormatException e)
        {
            return valueIfNotInt;
        }
    }
}
