{% highlight java %} /** * --------------------------------------------------------------------------- * File name: WhileLoopAnswers.java * Project name: Winter Study Guide * --------------------------------------------------------------------------- * Author’s name and email: Michael Schneider, schneidm@etsu.edu * Course-Section: All * Creation Date: 1/5/15 * Last modified: Schneider 1/5/15 schneidm@etsu.edu * --------------------------------------------------------------------------- */ import java.util.Scanner; /** * Class Name: WhileLoopAnswers
* Class Purpose: To give answers for the while loop exercises *
* *
* Date created: 1/5/15
* Last modified: Schneider 1/5/15 schneidm@etsu.edu * @author Michael Schneider */ public class WhileLoopAnswers{ public static void main(String[] args){ /******************************************************* * * Examples * ********************************************************/ //Create a while loop that displays 0 to 15 to the screen int counter = 0;//Set initial state while(counter <= 15)//Conditional statement { System.out.println(" " + counter); counter++;//Update counter } //Using a while loop, displays ("That number isn't 4!") until the user enters 4 Scanner kb = new Scanner(System.in); System.out.println("Enter the # 4"); int input = kb.nextInt();//Initial State while(input != 4)//Conditional Statement { System.out.println("That number isn't 4!"); input = kb.nextInt();//Update state } /********************************** * * Counting Exercises * ***********************************/ //Problem 1 //Create a while loop that counts from 1 to 10 counter = 1; while(counter < 11) { System.out.print(" " + counter); counter++; } //Problem 2 //Create a while loop that counts from 0 to 50 by counting by 5 counter = 0; while(counter < 51) { System.out.print(" " + counter); counter+=5; } //Problem 3 //Create a while loop that counts from 10 to 1 counter = 10; while(counter > 0) { System.out.print(" " + counter); counter--; } /********************************** * * Repetitive Exercises * ***********************************/ //Problem 1 //Sum all the numbers from 1 to 50 int sum = 0; counter = 1; while(counter < 51) { sum+=counter; counter++; } System.out.println("The total sum of 1 to 50 is: " + sum); //Problem 2 // Write the necessary code to do the following: // -Display the following menu to the user. // 1. Say "Hello" // 2. Say "Good Bye" // 3. Exit // // -Process the user's response // // -Display an error message if the user enters an invalid number // (i.e. a number not 1-3) // // -Keep asking the user for input until they enter 3 // System.out.println("Please choose an option from the menu below:"+ "\n 1. Say \"Hello\" "+ "\n 2. Say \"Good Bye \"" + "\n 3. Exit"); int menuChoice = kb.nextInt(); while(menuChoice != 3) { switch(menuChoice) { case 1: System.out.println("Hello!"); break; case 2: System.out.println("Good Bye"); break; default: System.out.println("Error... invalid input..."); } System.out.println("Please choose an option from the menu below:"+ "\n 1. Say \"Hello\" "+ "\n 2. Say \"Good Bye \"" + "\n 3. Exit"); menuChoice = kb.nextInt(); } System.out.println("You have exited the menu!"); } } {% endhighlight %}