|
|
Line 1: |
Line 1: |
− | ==Introduction==
| + | {{1010Topic}} |
− | The while loop is a special form of [[Loops|loop]] that will continue to repeat as long as the [[test condition]] evaluates to ''true''.
| + | |
− | | + | |
− | ==Syntax==
| + | |
− | In java, the while loop takes the form of
| + | |
− | <pre>
| + | |
− | while(test condition)
| + | |
− | { | + | |
− | ...
| + | |
− | statements
| + | |
− | ...
| + | |
− | }
| + | |
− | </pre>
| + | |
− | In this particular situation, the while loop will continue to iterate as long as the test condition evaluates to true
| + | |
− | | + | |
− | ==Uses==
| + | |
− | A while loop is useful in any situation where you don't know beforehand how often you want to repeat the block of code. Consider the following situation instance:
| + | |
− | | + | |
− | <pre>
| + | |
− | import javax.swing.JOptionPane;
| + | |
− | | + | |
− | public class Input
| + | |
− | { | + | |
− | public static void main(String args[])
| + | |
− | {
| + | |
− | String done;
| + | |
− | done = JOptionPane.showInputDialog("Enter a phrase, type quit to finish");
| + | |
− |
| + | |
− | while(!done.equals("quit"))
| + | |
− | {
| + | |
− | System.out.println("Hello " + name);
| + | |
− | done = JOptionPane.showInputDialog("Enter a phrase, type quit to finish");
| + | |
− | }
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | This code will repeatedly ask the user for input, and display that input, until the user types quit.
| + | |
− | | + | |
− | ==Assignments==
| + | |
− | Ignore the //<--Comment--> comments until you've read the assignment modification at the bottom
| + | |
− | | + | |
− | '''Asst. 1'''
| + | |
− | Guess my number program
| + | |
− | <pre>
| + | |
− | import javax.swing.JOptionPane;
| + | |
− | | + | |
− | public class GuessNumber
| + | |
− | {
| + | |
− | public static void main(String args[])
| + | |
− | {
| + | |
− | int myNumber = 29;
| + | |
− | String number;
| + | |
− | int guess;
| + | |
− | //<--Min and max variables go here-->
| + | |
− | | + | |
− | //get guess from user
| + | |
− | number = JOptionPane.showInputDialog("Pick a number between 1 and 100"); //<--Show user min and max-->
| + | |
− | | + | |
− | //convert number from string to integer
| + | |
− | guess = Integer.parseInt(number);
| + | |
− | while(guess != myNumber) {
| + | |
− | if(guess < myNumber)
| + | |
− | {
| + | |
− | //<--Modify min or max variables here-->
| + | |
− | number = JOptionPane.showInputDialog("Too low, please try again); //<--Show user min and max-->
| + | |
− | }
| + | |
− | else
| + | |
− | {
| + | |
− | //<--Modify min or max variables here-->
| + | |
− | number = JOptionPane.showInputDialog("Too high, please try again); //<--Show user min and max-->
| + | |
− | }
| + | |
− | | + | |
− | //convert number from string to integer
| + | |
− | guess = Integer.parseInt(number);
| + | |
− | }
| + | |
− | JOptionPane.showMessageDialog(null, "Congratulations, you picked the right number");
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | This program will ask the user to guess the number until the user guesses correctly. How can this program be improved?
| + | |
− | | + | |
− | Modify this program so that the program tells the user whether they guessed too high or too low. Hints have been placed in the program in comments.
| + | |
− | | + | |
− | [[While_Loops_Answers_1|Answer]]
| + | |
− | | + | |
− | '''Asst. 2'''
| + | |
− | Pick up stones
| + | |
− | | + | |
− | '''Note:''' May want to read up on [[test condition#Logical Operators|Logical Operators]] before doing this one.
| + | |
− | <pre>
| + | |
− | import javax.swing.JOptionPane;
| + | |
− | | + | |
− | public class Stones
| + | |
− | {
| + | |
− | public static void main(String args[])
| + | |
− | {
| + | |
− | int totalStones = 21;
| + | |
− | | + | |
− | String number;
| + | |
− | int removed;
| + | |
− | | + | |
− | //<--Uncomment when doing assignment-->
| + | |
− | //JOptionPane.showMessageDialog(null, "Pick up 1-3 stones until all are gone");
| + | |
− | | + | |
− | //get guess from user
| + | |
− | number = JOptionPane.showInputDialog("There are " + totalStones + " stones, pick up how many?");
| + | |
− | | + | |
− | //convert number from string to integer
| + | |
− | removed= Integer.parseInt(number);
| + | |
− | | + | |
− | //<--Ask the user for another input while removed > 3 or removed <= 0 or removed > totalStones-->
| + | |
− |
| + | |
− | //remove stones from pile
| + | |
− | totalStones -= removed;
| + | |
− | | + | |
− | while(totalStones > 0) {
| + | |
− | number = JOptionPane.showInputDialog("There are " + totalStones + " stones, pick up how many?");
| + | |
− | | + | |
− | //convert number from string to integer
| + | |
− | removed= Integer.parseInt(number);
| + | |
− | | + | |
− | //<--Ask the user for another input while removed > 3 or removed <= 0 or removed > totalStones-->
| + | |
− |
| + | |
− | //remove stones from pile
| + | |
− | totalStones -= removed;
| + | |
− | }
| + | |
− | JOptionPane.showMessageDialog(null, "All the stones are gone");
| + | |
− | }
| + | |
− | } | + | |
− | </pre>
| + | |
− | | + | |
− | This program allows the user to remove stones, as many as they want, until there are no stones remaining.
| + | |
− | | + | |
− | Modify this program so that the user cannot remove more than 3 stones at a time, and so that the user cannot remove more stones than there are available.
| + | |
− | | + | |
− | [[While_Loops_Answers_2|Answer]]
| + | |
− | | + | |
− | '''[[Loops|Back to Loops]]'''
| + | |