Created
November 30, 2012 21:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************** | |
* 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