Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to edit the following JAVA code so that if the entire input is 0, the pro

ID: 3822429 • Letter: I

Question

I need to edit the following JAVA code so that if the entire input is 0, the program displays "No numbers are entered except 0". Otherwise the outputs and functions should remain the same.

import java.util.Scanner;
import java.util.*;
import java.text.*;

public class Exercise05_01 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

//declare variagbles
int total = 0;
double average = 0.0;
int numOfNeg = 0;
int numOfPos = 0;
int numOfValues = 0;
int value;

//user input
System.out.print("Enter an integer, the input ends if it is 0: ");
do
{
//read all of the input numbers (unlimited until user prompts stop)
value = scan.nextInt();

//determine positive or neg
//add to count

//if positive
if (value>0){
numOfPos++;
numOfValues++;
total+=value;
}//end if positive
//if negative
else if(value<0){
numOfNeg++;
numOfValues++;
total+=value;
}//end if negative

//start loop if there is no zero
}
while (value != 0);
{

DecimalFormat dec=new DecimalFormat("#,###.##");

average=total/numOfValues;

System.out.println("The number of postitives is "+numOfPos);
System.out.println("The number of negatives is "+numOfNeg);
System.out.println("The total is "+total);;
System.out.println("The average is "+dec.format(average));
}
}
}

Explanation / Answer

Hi,I have modified the required functionality.

Please let me know in case of any issue.

import java.util.Scanner;

import java.util.*;

import java.text.*;

public class Exercise05_01 {

   /**

   * @param args the command line arguments

   */

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       //declare variagbles

       int total = 0;

       double average = 0.0;

       int numOfNeg = 0;

       int numOfPos = 0;

       int numOfValues = 0;

       int value;

       //user input

       System.out.print("Enter an integer, the input ends if it is 0: ");

       do

       {

           //read all of the input numbers (unlimited until user prompts stop)

           value = scan.nextInt();

           //determine positive or neg

           //add to count

           //if positive

           if (value>0){

               numOfPos++;

               numOfValues++;

               total+=value;

           }//end if positive

           //if negative

           else if(value<0){

               numOfNeg++;

               numOfValues++;

               total+=value;

           }//end if negative

           //start loop if there is no zero

       }

       while (value != 0);

      

       if(numOfValues > 0){

           DecimalFormat dec=new DecimalFormat("#,###.##");

           average=total/numOfValues;

           System.out.println("The number of postitives is "+numOfPos);

           System.out.println("The number of negatives is "+numOfNeg);

           System.out.println("The total is "+total);;

           System.out.println("The average is "+dec.format(average));

       }

   }

}

/*

Sample run:

Enter an integer, the input ends if it is 0: 0

Enter an integer, the input ends if it is 0: 4

12 3 4 0

The number of postitives is 4

The number of negatives is 0

The total is 23

The average is 5

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote