Using break and continue Examples for using the break and continue keywords: Example 3.5: TestBreak.java Example 3.6: TestContinue.java
// TestBreak.java: Test the break keyword in the loop,15 public class TestBreak { /** Main method */ public static void main(String[] args) { int sum = 0; int item = 0; while (item < 5) { item ++; sum += item; //if (sum >= 6) break; } System.out.println("The sum is " + sum); } }
// TestContinue.java: Test the continue keyword,13 public class TestContinue { /** Main method */ public static void main(String[] args) { int sum = 0; int item = 0; while (item < 5) { item++; if (item == 2) continue; sum += item; } System.out.println("The sum is " + sum);
3.7 Finding the Sales Amount You have just started a sales job in a department store. Your pay consists of a base salary and a commission. The base salary is $5,000. The scheme shown below is used to determine the commission rate. Sales Amount Commission Rate $0.01–$5,000 8 percent $5,000.01–$10,000 10 percent $10,000.01 and above 12 percent Your goal is to earn $30,000 in a year. Write a program that will find out the minimum amount of sales you have to generate in order to make $30,000.
// FindSalesAmount.java: Find the sales amount to get the // desired commission.Base salary 5000,earn30000 a yr. import javax.swing.JOptionPane; public class FindSalesAmount { /** Main method */ public static void main(String[] args) { // The commission sought final double COMMISSION_SOUGHT = 25000; final double INITIAL_SALES_AMOUNT = 0; double commission = 0; double salesAmount = INITIAL_SALES_AMOUNT; do { // Increase salesAmount by 1 cent salesAmount += 0.01;
// Compute the commission from the current salesAmount; if (salesAmount >= 10000.01) commission = 5000 * 0.08 + 5000 * 0.1 + (salesAmount - 10000) * 0.12; else if (salesAmount >= 5000.01) commission = 5000 * 0.08 + (salesAmount - 5000) * 0.10; else commission = salesAmount * 0.08; } while (commission < COMMISSION_SOUGHT); // Display the sales amount String output = "The sales amount $" + (int)(salesAmount * 100) / 100.0 + "\nis needed to make a commission of $" + COMMISSION_SOUGHT; JOptionPane.showMessageDialog(null, output, "Example 3.7 Output", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }
3.8 Displaying a Pyramid of Numbers In this example, you will use nested loops to print the following output: 1 212 32123 4321234 543212345 Your program prints five lines. Each line consists of three parts. The first part comprises the spaces before the numbers; the second part, the leading numbers, such as 3 2 1 on line 3; and the last part, the ending numbers, such as 2 3 on line 3.
// PrintPyramid.java: Print a pyramid of numbers public class PrintPyramid { /** Main method */ public static void main(String[] args) { final int NUM_OF_LINES = 5; for (int row = 1; row <= NUM_OF_LINES; row++) { // Print leading spaces for (int column = 1; column <= NUM_OF_LINES - row; column++) System.out.print(" "); // Print leading numbers for (int num = row; num >= 1; num--) System.out.print(num); // Print ending numbers for (int num = 2; num <= row; num++) // Start a new line System.out.println(); }
3.9 Displaying Prime Numbers This example displays the first 50 prime numbers in five lines, each of which contains 10 numbers. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. The problem can be broken into the following tasks: For number = 2, 3, 4, 5, 6, ..., test whether the number is prime. Determine whether a given number is prime. Count the prime numbers. Print each prime number, and print 10 numbers per line.