Java Reference
In-Depth Information
else
{
Process input
}
}
247
248
This pattern is sometimes called Ȓloop and a halfȓ. Some programmers find it clumsy
to introduce a control variable for such a loop. Advanced Topic 6.3 shows several
alternatives.
Let's put together the data analysis program. To decouple the input handling from the
computation of the average and the maximum, we'll introduce a class DataSet . You
add values to a DataSet object with the add method. The getAverage method
returns the average of all added data and the getMaximum method returns the
largest.
ch06/dataset/DataAnalyzer.java
1 import java.util.Scanner;
2
3 /**
4This program computes the average and maximum of a set
5of input values.
6 */
7 public class DataAnalyzer
8 {
9 public static void main(String[] args)
10 {
11 Scanner in = new Scanner(System.in);
12 DataSet data = new DataSet();
13
14 boolean done = false;
15 while (!done)
16 {
17 System.out.print(ÐEnter value,
Q to quit: Ñ);
18 String input = in.next();
19 if
(input.equalsIgnoreCase(ÐQÑ))
20 done = true;
21 else
22 {
Search WWH ::




Custom Search