Will not run in Java. New to programming, Please help. //DistanceFromAverage.jav
ID: 3600153 • Letter: W
Question
Will not run in Java. New to programming, Please help.
//DistanceFromAverage.java
import java.uti.Scanner;
public class DistanceFromAverage
{
public static void main(String[]args)
{
//declare scanner class object
Scanner keyword=new Scanner(System.in);
/*declare array of double type values and reserve memeory size of 20 double values*/
double[]input=new double[20];
//double variable to read value
double value;
//set repeat to true
boolean repeat=true;
//set sentinal value
int sentinal=99999;
//set count to zero
int counter=0;
//set total to zero
double total=0;
/iterate over loop that prompts user to enter values.If user enters 99999 exit from loop and print message "Exit without any input"*/
for(int index=0; index<input.length&&repeat; index++)
{
//prompt user inout value
System.out.println("enter input value or 99999 to exit");
value=keyword.nextDouble();
//check user inputvalue is sentinal value
if(value==sentinal)
repeat false;
else
{
//assign value into input array
input[index]=value;
//add total
total+=input[index];
//increment the counter by one
counter++;
}
{
//calculate average value
double average=total/counter;
//print message if user enters no value
if(counter==0)
System.out.println("exit without any input value");
else
{
/*print formatted value for input array and distance from average*/
System.out.println("%-10s%-20s/n","value","distance from average");
for(int i=0;i<counter; i++)
{
System.out.println("%-10.2f%-10.2f ",input[i],average-input[i];
}
}
}
}
Explanation / Answer
Note : Hey,I am just removing Errors in this Code,,I am not considering your logic what you are doing in this program..
This Code will not give any Error :
import java.util.Scanner;
public class DistanceFromAverage
{
public static void main(String[]args)
{
//declare scanner class object
Scanner keyword=new Scanner(System.in);
/*declare array of double type values and reserve memeory size of 20 double values*/
double[]input=new double[20];
//double variable to read value
double value;
//set repeat to true
boolean repeat=true;
//set sentinal value
int sentinal=99999;
//set count to zero
int counter=0;
//set total to zero
double total=0.0;
/*iterate over loop that prompts user to enter values.If user enters 99999 exit from loop and print message "Exit without any input"*/
for(int index=0; index<input.length&&repeat; index++)
{
//prompt user inout value
System.out.println("enter input value or 99999 to exit");
value=keyword.nextDouble();
//check user inputvalue is sentinal value
if(value==sentinal)
repeat=false;
else
{
//assign value into input array
input[index]=value;
//add total
total+=input[index];
//increment the counter by one
counter++;
}
//calculate average value
double average=(double)total/counter;
//print message if user enters no value
if(counter==0)
System.out.println("exit without any input value");
else
{
/*print formatted value for input array and distance from average*/
System.out.println("value"+" distance from average");
for(int i=0;i<counter; i++)
{
System.out.println(input[i]+" "+(average-input[i]));
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.