StefanTobler

Lesson 17 Activity 1

Oct 13th, 2016
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. /*
  2.  * Lesson 17 Coding Activity 1
  3.  * Write a program that will input a list of test scores in from the keyboard.
  4.  * When the user enters -1, print the average.
  5.  *
  6.  * What do you need to be careful about when using -1 to stop a loop?
  7.  *
  8.  *     Sample Run:
  9.  *         Enter the Scores:
  10.  *         45
  11.  *         100
  12.  *         -1
  13.  *
  14.  *         The average is: 72.5
  15.  *
  16.  *
  17.  */
  18.  
  19. import java.util.Scanner;
  20. import java.lang.Math;
  21.  
  22. class Lesson_17_Activity_One {
  23.     public static void main(String[] args)
  24.      {
  25.       Scanner scan = new Scanner(System.in);
  26.       System.out.println("Enter the Scores:");
  27.       int x = 0;
  28.       int count = -1;
  29.       double total = 1;
  30.         while ( x != -1)
  31.         {
  32.           x = scan.nextInt();
  33.           count++;
  34.           total = total + x;
  35.         }
  36.       System.out.println("The average is: " + (double)(total/count));
  37.        
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment