Why is this producing an infinite looP? import java.util.Scanner; //keyboard pub
ID: 3788703 • Letter: W
Question
Why is this producing an infinite looP?
import java.util.Scanner; //keyboard
public class DataSet { //class
public static void main(String[] args) {
double sum = 0;
int count = 0;
double largest = 0;
double smallest = 0;
double range = 0;
double value = 0;
double average = 0;
//public static void getAverage(double value){
System.out.println("Enter values, enter -1 to finish");
Scanner scan = new Scanner(System.in);
value = scan.nextDouble();
while (value !=-1){//this must come after the scan.
if (value !=-1){
sum = sum + value;
count++;
}
if (count>0){
average = sum/count;
System.out.println("The average is: " + average);}
else {
System.out.println("Sorry, that value is not accepted");}
//public void getLargest(double value){
if (value > largest)
largest = value;
System.out.println("The largest is: " + largest);
}
}
}
/*
}
public void getSmallest(double value){ //smallest
if (value < smallest)
smallest = value;
}
public void getRange(double value){
range = largest - smallest;
}
System.out.println("The smallest is: " + smallest);
System.out.println("The range is: " + range);
}
}
/*
while (value >=0){
//methods
public void add(double value){
//is this to append the different values?
{
*/
Explanation / Answer
Please find answers and observations below:
1.) Why is the program going into an infinite loop: The variable 'value' is set before starting while loop and value is not changed inside while loop. Hence once the variable is set to value other than -1, the condition becomes always true for every iteration hence preventing program from terminating.
2.) How to remove infinite loop: As per my understanding the goal of program is to take input from user and give average and largest values entered by user till that point. Once user enters -1 as value the program needs to terminate. We need to include statements to take input from users inside while loop also
3.) Please also note that first if control statement inside while loop with same condtion as of while loop i.e. (value !=-1) is redundant. Hence i have removed the statement in solution
==================================================================================
import java.util.Scanner; //keyboard
public class DataSet { //class
public static void main(String[] args) {
double sum = 0;
int count = 0;
double largest = 0;
double smallest = 0;
double range = 0;
double value = 0;
double average = 0;
//public static void getAverage(double value){
System.out.println("Enter values, enter -1 to finish");
Scanner scan = new Scanner(System.in);
value = scan.nextDouble();
while (value !=-1){//this must come after the scan.
sum = sum + value;
count++;
average = sum/count;
System.out.println("The average is: " + average);
//public void getLargest(double value){
if (value > largest)
largest = value;
System.out.println("The largest is: " + largest);
//Take input from user inside While Loop
System.out.println("Enter values, enter -1 to finish");
value = scan.nextDouble();
}
if(count<=0){System.out.println("Sorry, that value is not accepted");}
}
}
/*
}
public void getSmallest(double value){ //smallest
if (value < smallest)
smallest = value;
}
public void getRange(double value){
range = largest - smallest;
}
System.out.println("The smallest is: " + smallest);
System.out.println("The range is: " + range);
}
}
/*
while (value >=0){
//methods
public void add(double value){
//is this to append the different values?
{
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.