import java.util.Scanner; public class WhileEx { public static void main(String[] args) { // MAIN CODE STARTS HERE // make the user guess, using a loop control variable int sekrit = 47; int user = 0; boolean done = false; // controls loop while (!done) { user = promptInt("Guess?"); if (user == sekrit) { done = true; } } System.out.println("You guessed it!"); // make the user guess // user input directly controls loop System.out.println("Second game"); sekrit = 989; user = 0; // dummy value so loop starts while (user != sekrit) { user = promptInt("Guess?"); } System.out.println("You guessed it! Again!"); // read in numbers from the user // and print sum and average System.out.println("Average time!"); int sum = 0; int count = 0; user = promptInt("What number (negative to quit)"); // as long as they give non-negative while (user >= 0) { sum = sum + user; // add last user input to sum count = count + 1; // update count // read in next input (or negative to quit) user = promptInt("Next number?"); } // just in case their first input was negative... if (count != 0) { double average = sum / count; System.out.println("Sum: " + sum); System.out.println("Average: " + average); } else { System.out.println("No numbers given"); } // MAIN CODE ENDS HERE } /* Simple Utility methods that prompt the user and return input If you enter the wrong type of input, the program will end with an error! */ // utility for reading in integer input public static int promptInt(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); int response = scan.nextInt(); return response; } }