RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsObject120 + its docsjava.lang docsjava.util docs

lect04c
if-else-if

    /** Return a greeting, selected randomly from a list of several greetings.
     * @return A greeting (randomly selected, not necessarily uniformly).
     */
    static String greet() {
        int choice = randomInt(100);
        // Object120.randomInt(n) returns a number in [0,n) = {0,1,2,...,n-1}.

        String msg;

        if (choice < 30) {
            msg = "Hello.";
        } else if (choice < 33) {  
            msg = "Aloha.";
        }
        else if (choice < 50) {  
            msg = "Buenos dias, amigo/amiga.";
        }
        else if (choice < 99) {  
            msg = "Yo.";
        }
        else if (choice < 100) {  
            // Not an advisable greeting.  Use sparingly.
            msg = "I am a javabot: System dot out dot println open \"hello\" close.";
        }
        else {
            System.err.println( "Uh-oh, I didn't expect to reach here!" );
            msg = "A dummy initialiazation for `msg`, to appease the compiler.";
        }

        return msg;
    }

homeinfolectslabsexamshws
tutor/PIsObject120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Sep.29 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme