Allow a user to enter any number of double values up to 20. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average. im using cengage and its only 15% correct but it works?import java.util.Scanner;public class DistanceFromAverage{public static void main(String[] args){double[] input = new double[20];int sentinal = 99999;double value;double total =0;int counter =0;boolean repeat=true;Scanner keyboard = new Scanner(System.in);// read valuesfor (int i = 0; i < input.length && repeat; i++) {System.out.print("Enter a value(99999 to exit):");value = keyboard.nextDouble();// breaks when values is 99999if (value == sentinal) {repeat=false;} else {input[i] = value;total += input[i];counter++;}}double average= total/counter;if (counter == 0)System.out.println("Error! Please enter any values");else {// calculate averageSystem.out.printf("%-10s%-20s\n","value","Distance from average");// calculate distancefor (int a = 0; a < counter; a++) {System.out.printf("%-10.2f%-10.2f\n",input[a],average-input[a]);}}}}

Respuesta :

Answer:

import java.util.Scanner;

public class DistanceFromAverage

{

static double[] userValues = new double[20];

static double userInput;

static int totalValues = 1;

static int y = 0;

static double totalUserInput = 0;

static double avg;

public static void main(String[] args)

{

getInfoFromUser();

calculation();

}

public static void getInfoFromUser() {

 

Scanner input = new Scanner(System.in);

System.out.print("Enter an integer or enter 99999 to quit: ");

userInput = Double.parseDouble(input.nextLine());

if(userInput == 99999)

System.out.println("Enter a value: ");

while(userInput != 99999 && totalValues < userValues.length)

{

userValues[totalValues] = userInput;

totalUserInput = totalUserInput + userInput;

System.out.print("Enter an integer or enter 99999 to quit: ");

userInput = Double.parseDouble(input.nextLine());

if(userInput == 99999)

break;

totalValues++;

}

}

public static void calculation() {

avg = totalUserInput / totalValues;

System.out.println("You have entered " + totalValues + " numbers and their avg is " +avg);

for (y = 1; y < totalValues; ++y)

{

System.out.println(userValues[y] + " is " + (userValues[y] - avg) + " away from the avg");

}

}

}

Explanation:

  • Create the getInfoFromUser method that takes information from the user and save it in the array and if user enters 99999 then it exits
  • Stop taking information from user if it exceeds 20  inputs.
  • Create the calculation method that finds the average and as well as calculates the distance from the average.
  • Calculate the average by using the following formula:

avg = totalUserInput / totalValues

  • At the end, display all the results.