AllTheTimeWorld.com

alice calculationsLoop to add numbers

Loop to add numbers

A loop to add numbers uses a running total: total=total + number

Loop to add numbers

A loop to add numbers uses a running total: total=total + number

This example uses a loop to let the user enter values and displays the total at the end.

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;     
   Double number = this.witch.getDoubleFromUser( "Enter the first number." );     
   while ( number>0.0 ) {         
      //Notice that you will never be at this sttment if number is zero         
      total = total+number;         
      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 ) ); }