final int LIMIT = 15; num sum int num = 1; --- --- int sum = 0; do { num = num + 3; sum = sum + num; } while (num < LIMIT); System.out.println ("Last number is " + num + " and sum is " + sum);
String str; int length; System.out.print ("Enter a string of characters: "); str = scan.nextLine(); // Find the length of str ________________________________________________________________; // Write a for loop to print the string backwards
final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int star = 1; star <= row; star++) System.out.print ("*"); System.out.println(); }
final int MAX_ROWS = 4; for (int row = 1; row <= MAX_ROWS; row++) { for (int dash = MAX_ROWS; dash > row; dash--) System.out.print ("-"); for (int star = 1; star <= row; star++) System.out.print ("*"); System.out.println(); }
TRACE of Variables row dash star Pattern Printed ------------------------
int num; String response; int sumPositive = 0; int countPositive = 0; do { // Prompt for and read in the number System.out.print ("Enter a number: "); num = scan.nextInt(); // Count and add the number if it is positive if (num > 0) { countPositive++; sumPositive += num; } // Update the smallest // Update the loop control (see if the user has more numbers) System.out.println ("Do you have another number?"); scan.nextLine(); // read the new line still in the input stream response = scan.nextLine(); // read the response } while (response.equalsIgnoreCase ("y")); // Print results if (countPositive > 0) System.out.println ("The average of the positive values is " + (sumPositive/countPositive)); else System.out.println ("There were no positive numbers entered.");