/** * Name: Erica Eddy * Course: CSCI 241 - Computer Science I * Section: 001 or 002 * Assignment: 1 */ import java.util.*; // for Scanner class public class PrintBoolean { public static void main (String [] args) { Scanner input = new Scanner(System.in); //----------------------------------------- // using if and mod //----------------------------------------- System.out.println("-----using if and mod-----"); System.out.println(); int x = 0; System.out.print("Enter an integer: "); x = input.nextInt(); if (x % 2 == 1) { // x is an odd number System.out.println("x (" + x + ") is an odd number"); } else // x is an even number System.out.println("x (" + x + ") is an even number"); //----------------------------------------- // using a boolean variable //----------------------------------------- System.out.println(); System.out.println(); System.out.println("-----using a boolean variable-----"); System.out.println(); boolean xIsOdd; x++; // x now has the value 6 (if you started with 5) if (x % 2 == 1) // x is an odd number xIsOdd = true; else // x is an even number xIsOdd = false; System.out.println("x = " + x + ", xIsOdd = " + xIsOdd); if (xIsOdd) System.out.println("x is an odd number"); else System.out.println("x is an even number"); //----------------------------------------------------- // setting boolean variable directly with an expression //----------------------------------------------------- System.out.println(); System.out.println(); System.out.println("-----set boolean variable directly-----"); System.out.println(); x++; // x now has the value 7 xIsOdd = (x % 2 == 1); System.out.println("x = " + x + ", xIsOdd = " + xIsOdd); if (xIsOdd) System.out.println("x is an odd number"); else System.out.println("x is an even number"); } }