Generate a random character(Nested if- else condition)

Generate a random character(Nested if- else condition)

Previous Home Next

 

Following program for Generate a random character ( using Nested if- else condition).

 

 

/*
 * Save as a LetterCheck.java
 * Program for Generate a random character.
 * For generate the different value re-run the program again.
 */
package r4r.co.in;

public class LetterCheck {

    public static void main(String args[]) {

        /*
         * Here, Using the Nested If-else condition
         * The Alahabet range(65 to 120)in ASCII encode
         * A-Z belong to 65- 90 and a-z belong to 97- 122
         * Math.random() used to Generate a random character( range 0 to 125)
         */

        char symbol = 'A';
        symbol = (char) (125.0 * Math.random());


        if (symbol >= 'A') // It is A or greater?
        {
            if (symbol <= 'Z') // if yes, so is it Z or less?
            {
                System.out.println("Generated value be capital letter " + symbol);
            } else // It is greater than Z
            if (symbol >= 'a') // So is it a or greater?
            {
                if (symbol <= 'z') // if Yes, so is it z or less?
                {
                    System.out.println("Generated value be small letter " + symbol);
                } else // It is not less than z
                {
                    System.out.println("Generated value would be Upper Numeric Letter " + symbol);
                }
            } else {
                System.out.println("Generated value would be Lower Numeric Letter " + symbol);
            }
        } else {
            System.out.println("Don't Generate any value" + symbol);
        }
    }
}

Generated value be capital letter Q
Previous Home Next