{% highlight java %} /** * --------------------------------------------------------------------------- * File name: WhileLoopExercises.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: WhileLoopExercises
* Class Purpose: To give example exercises on using while loop to create repetitive code *
* *
* Date created: 1/5/15
* Last modified: Schneider 1/5/15 schneidm@etsu.edu * @author Michael Schneider */ public class WhileLoopExercises{ 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 //Problem 2 //Create a while loop that counts from 0 to 50 by counting by 5 //Problem 3 //Create a while loop that counts from 10 to 1 /********************************** * * Repetitive Exercises * ***********************************/ //Problem 1 //Sum all the numbers from 1 to 50 //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 // } } {% endhighlight %}