LoopTypes - double average count = 0 average = 0...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.Scanner; public class LoopTypes { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //Loop types: //counter-controlled (for) //ex:find eaveage of 10 integers /*final int SIZE = 10; // final variable, named constant, constant variable double n1, avg; avg = 0; for (int count=1; count<10; count++) { System.out.print("enter integer: "); n1 = keyboard.nextInt(); avg += n1; } avg = avg / SIZE; System.out.println("average is: %.3f\n", avg); }*/ //sentinel-controlled (while, do while) //ex: find the average of some positive integers (sentinel controlled) /*String response; int count, num;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: double average; count = 0; average = 0; System.out.println("Would you like to play? Y / N"); response = keyboard.nextLine(); while (response.equals("Y") || response.equals("y")) { if (response.equals("Y") || response.equals("y")) { System.out.print("Enter a number, or any letter (besides Y) to end:"); num = keyboard.nextInt(); average +=num; count++; System.out.print("Would you like to continue?"); response = keyboard.nextLine(); } } System.out.println("Average is:" + (average / count)); */ //event-controlled (while, do while) //ex: find the average of numbers until end of input } }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online