Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Created November 30, 2012 21:38
Show Gist options
  • Save 0x000000AC/4178853 to your computer and use it in GitHub Desktop.
Save 0x000000AC/4178853 to your computer and use it in GitHub Desktop.
Uses a "sentinal value" to exit the loop. The program poorly creates an average score.
/***********************************************
* WhileLoopSentinalValue.java
* Aaron P. Clark
*
* Based on the pseudocode while loop on p.40 in Ch 2.
* Takes scores and computes average.
***********************************************/
import java.util.Scanner;
public class WhileLoopSentinalValue
{
public static void main(String[] args)
{
//Declares the y/n in repeat and the user input values
String repeat;
int totalScore;
int count;
int average;
int score;
count = 0;
totalScore = 0;
average = 0;
score = 0;
Scanner keyboard = new Scanner(System.in);
Scanner in = new Scanner(System.in);
System.out.println("Enter score (-1 to quit) ");
score = keyboard.nextInt();
while (score != -1)
{
totalScore = totalScore + score;
count++;
System.out.println("Enter score (-1 to quit) ");
score = in.nextInt();
average = (int) (totalScore/count);
System.out.println("The average score is: " + average);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment