Zebra0.com

alice calculationsCalculations and Functions

Calculations and Functions

This example uses a loop to let the user enter values and displays the total at the end. It also finds the smallest and largest.

Download witch-total.a3p

The code below is Java Code. You can see the Java Code by selectin Window -> Preferences -> Java Code.

public void myFirstMethod() {
    //Programmer : Janet Joy
    //User enters numbers and they are added to total.
    this.witch.say( "I will add some numbers for you." );
    Double total = 0.0;
    Integer count = 0;
    Double number = this.witch.getDoubleFromUser( "Enter the first number." );
    Double smallest = number;
    Double largest = number;
    while ( number>0.0 ) {
        //Notice that you will never be at this sttment if number is zero
        total = total+number;
        count = count+1;
        if( number<smallest ) {
            smallest = number;
        } else {
        }
        if( number>largest ) {
            largest = number;
        } else {
        }
        this.witch.say( "The total so far is "+total, Say.duration( 1.0 ) );
        number = this.witch.getDoubleFromUser( "Enter the next number or 0 to end : " );
    }
    this.witch.say( "The total is "+total+" Big deal!", Say.duration( 1.0 ) );
    this.witch.say( "The numbers ranged from "+smallest+" to "+largest );
} 

Next: Self Study Questions