46 lines
1.2 KiB
Java
46 lines
1.2 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package exam2_calebfontenot;
|
|
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
*
|
|
* @author ar114
|
|
*/
|
|
public class Problem1 {
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
public static void main(String[] args) {
|
|
// Define variables
|
|
double userInput = 0, average = 0;
|
|
int timesRun = 0;
|
|
|
|
// Create scanner
|
|
Scanner input = new Scanner(System.in);
|
|
|
|
System.out.println("I will calculate the average of numbers you will enter. \n");
|
|
|
|
// While loop to grab inputs and calculate
|
|
while (userInput != -1) {
|
|
System.out.print("Enter a positive number, or type -1 to quit: ");
|
|
userInput = input.nextDouble();
|
|
|
|
if (userInput != -1) {
|
|
// Calculate average
|
|
average += userInput;
|
|
// Increment timesRun
|
|
timesRun++;
|
|
}
|
|
}
|
|
average = average / (double) timesRun;
|
|
System.out.println("The average is: " + average);
|
|
}
|
|
|
|
}
|