/** * Loop with complicated exit condtion * @author Pete Nordquist * @version 100114 */ public class W03N01 { public static void main(String[] args) { // Example: // Write a loop that reads and accumulates numbers until the // the user enters 10 numbers, or // the user enters a negative number. // print the sum at the end // count number of times through the loop int counter; // the current sum double accumulator; // value read from user double userValue; counter = 0; accumulator = 0.0; // initialize userValue with a value that will get us into the loop // the first time userValue = 0.0; // start loop // enter loop as long as userValue not negative or counter < 10 while (userValue >= 0.0 && counter < 10) { System.out.print("your number> "); userValue = TextIO.getlnDouble(); // accumulate and count only if the user entered a good value if (userValue >= 0) { // add the value read to the acculator accumulator = accumulator + userValue; // increment counter counter = counter + 1; } } System.out.println("Accumulated sum is " + accumulator); } }