178k views
2 votes
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\\","value","Distance from average");// calculate distancefor (int a = 0; a < counter; a++) {System.out.printf("%-10.2f%-10.2f\\",input[a],average-input[a]);}}}}

User Tresstylez
by
4.2k points

2 Answers

2 votes

Final answer:

The program's objective is to record up to 20 double values, compute, and then display each value's distance from their average, handling scenarios when no values are entered. The importance of interpreting the calculator's output in the context of significant figures should also be considered.

Step-by-step explanation:

The task involves writing a program that allows a user to enter up to 20 double values with an option to exit using a sentinel value of 99999. The program calculates and presents each entered value along with its distance from the average. It appears the code you've posted might have some logic issues, particularly with handling the scenario when no numbers are entered before quitting (counter equals 0).

When calculating the distance from the average, note that this distance is the absolute difference between the entered number and the computed average. The code should ensure that the average is only calculated if at least one number has been entered to avoid division by zero, which can be done with an if statement checking whether counter > 0 before calculating the average. If no numbers are entered, an error message is displayed.

Remember that calculators and programs will display as many decimal places as they can compute, but it's up to the individual to interpret the results according to the significance of the figures. This can be handled in Java by setting the required number of decimal places in the printf method, or by rounding the numbers as necessary.

User Jaco
by
4.4k points
0 votes

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");

}

}

}

Step-by-step 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.
User ZombieBatman
by
5.0k points