Page 159 - Introduction to Programming with Java: A Problem Solving Approach
P. 159

                Figure 4.11 BridalRegistry program with while loop and user-query terrmination
Infinite Loops
Suppose you’re trying to print the numbers 1 through 10. Will the following code fragment work?
int x = 0;
while (x < 10)
{
}
System.out.println(x + 1);
4.8 while Loop 125
 /*************************************************************
*
BridalRegistry.java
Dean & Dean
This makes entries in a bridal registry.
*************************************************************/
*
*
*
import java.util.Scanner;
public class BridalRegistry
{
}
// end BridalRegistry class
public static void main(String[] args)
{
}
// end main
Scanner stdIn = new Scanner(System.in);
String registry = "";
char more;
System.out.print(
"Do you wish to create a bridal registry list? (y/n): ");
more = stdIn.nextLine().charAt(0);
while (more == 'y')
{
System.outA.praingt(o"EntPerDiFtem:En")h; ancer registry += stdIn.nextLine() + " - "; System.out.print("Enter store: ");
registry += stdIn.nextLine() + "\n"; System.out.print("Any more items? (y/n): "); more = stdIn.nextLine().charAt(0);
// end while
if (!registry.equals(""))
}
{
}
System.out.println("\nBridal Registry:\n" + registry);
























































   157   158   159   160   161