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

Ask a homework question - tutors are online