Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 6th Edition
by Barry Burd

Chapter 11: Around and Around It Goes

In this chapter:

Living large

import java.util.Scanner;

public class LivingLarge {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int input;
        
        input = keyboard.nextInt();
        while (input <= 100) {
            input = keyboard.nextInt();
        }
        
        keyboard.close();
    }

}

Are we there yet?

import java.util.Scanner;

public class AreWeThere {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        char reply;
        
        System.out.print("Are we there yet? ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        
        while (reply != 'Y' && reply != 'y') {
            System.out.print("Are we there yet? ");
            reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        }
        
        System.out.println("Whew!");
        
        keyboard.close();
    }

}

Tally up

import java.util.Scanner;

public class Tally {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int input;
        int sum = 0;
        
        input = keyboard.nextInt();
        while (input >= 0) {
            sum += input;
            input = keyboard.nextInt();
        }
        
        System.out.println(sum);
        
        keyboard.close();
    }

}

Guess again

import java.util.Random;
import java.util.Scanner;

class GuessingGame {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        Random myRandom = new Random();
        int randomNumber;
        int guess;
        
        randomNumber = myRandom.nextInt(10) + 1;

        System.out.print("Guess a number from 1 to 10: ");
        guess = keyboard.nextInt();
        
        while (guess != randomNumber) {
            System.out.print("Guess a number from 1 to 10: ");
            guess = keyboard.nextInt();
        }
        
        keyboard.close();
    }

}

Two in a row

import java.util.Scanner;

public class TwoInARow {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int previous, current;
        
        previous = keyboard.nextInt();
        current = keyboard.nextInt();
        
        while (current != previous) {
            previous = current;
            current = keyboard.nextInt();
        }
        
        System.out.println("Done!");
        
        keyboard.close();
    }

}