Monday, 12 January 2015

// The "Evens_Revised" class.
// (from the book:  A Guide to Programming in Java)

// This program accepts a positive or negative integer.
// Then, a ''modulus 2'' operation is performed,
// An appropriate message is displayed depending
// on whether the input integer was odd or even.

import java.awt.*;
import hsa.Console;

public class Evens_Revised
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        int number;
        int answer;
        char response = 'y';

        do
        {

            c.println (" Enter a value that is + or -");
            number = c.readInt ();
            answer = number % 2;

            if (answer == 0)
            {

                c.println ("This is: " + number);
                c.println ("Congratulations! You've finally printed out an even number!");


            }
            else
            {
                c.println ("The result of " + number + " modulus 2 is "
                        + Math.abs (answer));
            }

            c.println ("Do you want to continue? (Y/N)");
            response = c.readChar ();
            if (response == 'N' || response == 'n')
            {
                break;
            }
        }
        while (response == 'y' || response == 'Y');
        // end of do-while loop
        c.println ();
        c.println ("Thanks for running the 'Evens_Revised' Java program!");

    } // main method
} // Evens_Revised class

No comments:

Post a Comment