Solutions to the Try It Out Exercises in Java For Dummies, 7th Edition
by Barry Burd

Chapter 6: Controlling Program Flow with Loops

In this chapter:

Randomly generated number is a number from 1 and 100

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;

public class GuessAgain {

    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);

        int numGuesses = 0;
        int randomNumber = new Random().nextInt(100) + 1;

        out.println("       ************         ");
        out.println("Welcome to the Guessing Game");
        out.println("       ************         ");
        out.println();

        out.print("Enter an int from 1 to 100: ");
        int inputNumber = keyboard.nextInt();
        numGuesses++;

        while (inputNumber != randomNumber) {
            out.println();
            if (inputNumber < randomNumber) {
              out.println("Try again with a higher number ...");
            } else {
              out.println("Try again with a lower number ...");
            }
            out.print("Enter an int from 1 to 100: ");
            inputNumber = keyboard.nextInt();
            numGuesses++;
        }

        out.print("You win after ");
        out.println(numGuesses + " guesses.");

        keyboard.close();
    }
}

Display the largest number that the user typed

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter a number (non-positive to quit): ");
    int number = keyboard.nextInt();

    int largest = number;

    while (number > 0) {
      if (number > largest) {
        largest = number;
      }
      System.out.print("Enter a number (non-positive to quit): ");
      number = keyboard.nextInt();
    }

    if (largest > 0) {
      System.out.println("The largest is " + largest);
    } else {
      System.out.println("You didn't enter any positive numbers.");
    }
    keyboard.close();
  }

}

A for statement's initialization may have several parts

jshell> import static java.lang.System.out

jshell> for (int i = 0, j = 10; i < j; i++, j--) {out.println(i + " " + j);}
0 10
1 9
2 8
3 7
4 6

Output of a total loop

45

Factorial

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int n = keyboard.nextInt();

    int factorial = 1;
    for (int i = 1; i <= n; i++) {
      factorial *= i;
    }

    System.out.println(n + "! is " + factorial);
    keyboard.close();
  }
}

Output of the first code example that prints asterisks

*****
*****
*****
*****
*****

Output of the second code example that prints asterisks

*
**
***
****
*****

Display three copies of a triangle pattern of asterisks

public class Snippet {

  public static void main(String[] args) {

    for (int copy = 0; copy < 3; copy++) {
      for (int row = 0; row < 5; row++) {
        for (int column = 0; column <= row; column++) {
          System.out.print("*");
        }
        System.out.println();
      }
    }

  }
}

Break out of a loop

Enter an int value: 5
5
Enter an int value: 3
3
Enter an int value: -6
-6
Enter an int value: 0
Done!

continue in a loop</h3>
Enter an int value: 100
Enter an int value: 500
Enter an int value: 9
9
Enter an int value: -1
-1
Enter an int value: 0
Done!

Does the modified code with a do loop instead of a while loop work correctly?

The modified code doesn't work correctly because it doesn't check the user's first input. No matter what the user enters for the first input, the modified code asks for another input. Here's an example:
Enter an int from 1 to 10: 7

Try again...
Enter an int from 1 to 10: 7
You win after 2 guesses.