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

Chapter 6: Controlling Program Flow with Loops

In this chapter:

Bigger and Better

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

public class GuessAgain {

    public static void main(String[] args) {
        var 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();
    }
}

No Negativity

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    var 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();
  }

}

For Amour

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

Collecting Values

45

Factorial

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    var 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();
  }
}

Seeing Stars

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

Seeing More and More Stars

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

Three Triangles

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 <= 4 - row; column++) {
          System.out.print("*");
        }
        System.out.println();
      }
    }

  }
}

Missed Opportunity

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.

Let's Bust Out of Here

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

Carry On and Keep Coding

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!