Java Program Sentinel Control Loop
Use in Program Eclipse
import java.util.Scanner; public class SentinelControlLoop { /** * * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner myInput = new Scanner(System.in); //Declare Variables int num, sum = 0, max, min, count =0; double average = 0.0; final int Sentinel = -9999; //Priming Stage System.out.print("Enter an integer value, or -9999 to quit: "); num = myInput.nextInt(); //intitalize max and min max = min = num; //WHILE Loop while (num != Sentinel) { //body of loop //Update Sum sum = sum + num; //sum += System.out.println("Sum is now "+ sum); if (num > max) max = num; if (num < min) min = num; //increment count of data items read count++; //Same as count = count + 1; System.out.print("num = " + num + " Count = " + count ); System.out.print(" Enter an integer value, or -9999 to quit: "); num = myInput.nextInt(); } if (count != 0) { average = (double)sum / count; System.out.println("Average is " + average); System.out.println (" max = "+ max + ", min " + min); } else System.out.println("Cant average 0 items "); } }-->
No comments:
Post a Comment