MP3 complete!
This commit is contained in:
@@ -12,7 +12,8 @@ import java.util.Scanner;
|
||||
*/
|
||||
public class Stats {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Create scanner
|
||||
Scanner input = new Scanner(System.in);
|
||||
// Define variables
|
||||
@@ -25,32 +26,42 @@ public class Stats {
|
||||
System.out.println("Press Y/y to enter a series of numbers or Q/q to quit.");
|
||||
System.out.println("Current sum: " + inputSum);
|
||||
userInput = input.next();
|
||||
if (userInput.toLowerCase().equals("y")) {
|
||||
try {
|
||||
do {
|
||||
System.out.print("Please enter a series of numbers; Type '-1' to quit (no quotes, you doofus!): ");
|
||||
userInput = input.next();
|
||||
if (Double.parseDouble(userInput) != -1) {
|
||||
inputSum += Double.parseDouble(userInput);
|
||||
inputPow += Math.pow(Double.parseDouble(userInput), 2);
|
||||
numberOfTimesRun++; // counter to store number of times we've added to input sum
|
||||
}
|
||||
System.out.println("Current sum: " + inputSum);
|
||||
System.out.println("Input sum^2: " + inputPow);
|
||||
} while (Double.parseDouble(userInput) != -1);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid input!");
|
||||
}
|
||||
if (userInput.toLowerCase().equals("y")) {
|
||||
try {
|
||||
do {
|
||||
System.out.print("Please enter a series of numbers; Type '-1' to quit (no quotes, you doofus!): ");
|
||||
userInput = input.next();
|
||||
if (Double.parseDouble(userInput) != -1) {
|
||||
inputSum += Double.parseDouble(userInput);
|
||||
inputPow += Math.pow(Double.parseDouble(userInput), 2);
|
||||
numberOfTimesRun++; // counter to store number of times we've added to input sum
|
||||
}
|
||||
if (numberOfTimesRun == 0) {
|
||||
System.out.println("No numbers entered!");
|
||||
}
|
||||
System.out.println("Current sum: " + inputSum);
|
||||
System.out.println("Input sum^2: " + inputPow);
|
||||
} while (Double.parseDouble(userInput) != -1);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid input!");
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println("Invalid input!");
|
||||
}
|
||||
// Calculate mean
|
||||
mean = inputSum / numberOfTimesRun;
|
||||
// Calculate deviation
|
||||
deviation = ((numberOfTimesRun - 1) / (inputPow - (Math.pow(inputSum, 2) / numberOfTimesRun)));
|
||||
|
||||
System.out.println();
|
||||
System.out.println("The mean is: " + mean);
|
||||
System.out.println("The deviation is: " + deviation);
|
||||
// Calculate mean
|
||||
mean = inputSum / numberOfTimesRun;
|
||||
// Calculate deviation
|
||||
if (numberOfTimesRun == 1) { // if the loop has only run once, don't bother calculating it, because the deviation is zero.
|
||||
deviation = 0;
|
||||
} else {
|
||||
deviation = Math.sqrt((inputPow - (Math.pow(inputSum, 2) / numberOfTimesRun)) / (numberOfTimesRun - 1));
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("The mean is: " + mean);
|
||||
System.out.println("The deviation is: " + deviation);
|
||||
|
||||
} while (!(userInput.toLowerCase().equals("q")));
|
||||
}
|
||||
while (!(userInput.toLowerCase().equals("q")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user