Question: v // P_2.java - Read and average 5 integers, print the result. import java.util.Scanner; public class P_2 { public static void main(String args[]) { int

v

// P_2.java - Read and average 5 integers, print the result.

import java.util.Scanner;

public class P_2

{

public static void main(String args[]) { int x; int count = 0; // (1) initialize a counter to 0 to count number of grades double sum = 0; // initialize the sum to 0 to make sure the sum at the beginning is 0 double average;

Scanner input = new Scanner(System.in);

// prompt the user: System.out.print( "Enter five grades separated by a single space, then press : "); while( count < 5) // (2) read five grades and compute their sum { // read each number and compute the sum: x = Integer.parseInt(input.nextInt()); sum = sum + x; count++; // (3) update the count } System.out.println("");

average = sum/5; // compute the average, total divided by the number of grades System.out.println( "The average is " + average);

return 0; }

}

There are two types of loops. The first type are controlled by a counter and they are referred to as "count-controlled" loops. The second type are controlled by a particular event and they are referred to as "event-controlled" loops. In either case, there is always a boolean expression that leads the flow of the computation into the body of the loop or out of it. To explain the difference between these two types of loops, let's look at the following two examples:

// P_2.java - Read and average 5 integers, print the result.

import java.util.Scanner;

public class P_2

{

public static void main(String args[]) { int x; int count = 0; // (1) initialize a counter to 0 to count number of grades double sum = 0; // initialize the sum to 0 to make sure the sum at the beginning is 0 double average;

Scanner input = new Scanner(System.in);

// prompt the user: System.out.print( "Enter five grades separated by a single space, then press : "); while( count < 5) // (2) read five grades and compute their sum { // read each number and compute the sum: x = Integer.parseInt(input.nextInt()); sum = sum + x; count++; // (3) update the count } System.out.println("");

average = sum/5; // compute the average, total divided by the number of grades System.out.println( "The average is " + average);

return 0; }

}

There are two types of loops. The first type are controlled by a counter and they are referred to as "count-controlled" loops. The second type are controlled by a particular event and they are referred to as "event-controlled" loops. In either case, there is always a boolean expression that leads the flow of the computation into the body of the loop or out of it. To explain the difference between these two types of loops, let's look at the following two examples:

// P_2.java - Read and average 5 integers, print the result.

import java.util.Scanner;

public class P_2

{

public static void main(String args[]) { int x; int count = 0; // (1) initialize a counter to 0 to count number of grades double sum = 0; // initialize the sum to 0 to make sure the sum at the beginning is 0 double average;

Scanner input = new Scanner(System.in);

// prompt the user: System.out.print( "Enter five grades separated by a single space, then press : "); while( count < 5) // (2) read five grades and compute their sum { // read each number and compute the sum: x = Integer.parseInt(input.nextInt()); sum = sum + x; count++; // (3) update the count } System.out.println("");

average = sum/5; // compute the average, total divided by the number of grades System.out.println( "The average is " + average);

return 0; }

}

There are two types of loops. The first type are controlled by a counter and they are referred to as "count-controlled" loops. The second type are controlled by a particular event and they are referred to as "event-controlled" loops. In either case, there is always a boolean expression that leads the flow of the computation into the body of the loop or out of it. To explain the difference between these two types of loops, let's look at the following two examples:

// P_2.java - Read and average 5 integers, print the result.

import java.util.Scanner;

public class P_2

{

public static void main(String args[]) { int x; int count = 0; // (1) initialize a counter to 0 to count number of grades double sum = 0; // initialize the sum to 0 to make sure the sum at the beginning is 0 double average;

Scanner input = new Scanner(System.in);

// prompt the user: System.out.print( "Enter five grades separated by a single space, then press : "); while( count < 5) // (2) read five grades and compute their sum { // read each number and compute the sum: x = Integer.parseInt(input.nextInt()); sum = sum + x; count++; // (3) update the count } System.out.println("");

average = sum/5; // compute the average, total divided by the number of grades System.out.println( "The average is " + average);

return 0; }

}

There are two types of loops. The first type are controlled by a counter and they are referred to as "count-controlled" loops. The second type are controlled by a particular event and they are referred to as "event-controlled" loops. In either case, there is always a boolean expression that leads the flow of the computation into the body of the loop or out of it. To explain the difference between these two types of loops, let's look at the following two examples

// P_2.java - Read and average 5 integers, print the result.

import java.util.Scanner;

public class P_2

{

public static void main(String args[]) { int x; int count = 0; // (1) initialize a counter to 0 to count number of grades double sum = 0; // initialize the sum to 0 to make sure the sum at the beginning is 0 double average;

Scanner input = new Scanner(System.in);

// prompt the user: System.out.print( "Enter five grades separated by a single space, then press : "); while( count < 5) // (2) read five grades and compute their sum { // read each number and compute the sum: x = Integer.parseInt(input.nextInt()); sum = sum + x; count++; // (3) update the count } System.out.println("");

average = sum/5; // compute the average, total divided by the number of grades System.out.println( "The average is " + average);

return 0; }

}

There are two types of loops. The first type are controlled by a counter and they are referred to as "count-controlled" loops. The second type are controlled by a particular event and they are referred to as "event-controlled" loops. In either case, there is always a boolean expression that leads the flow of the computation into the body of the loop or out of it. To explain the difference between these two types of loops, let's look at the following two examples:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!