Turtlegraphics with Java
HomeAufgabenDruckenJava-Online

switch-structure (Multiple selection)

Instead of using several nested if-else-structucres, the switch-structure is often used. The switch-value is compared to the available values and the corresponding case()-action is executed. Each case-action must be ended with the statement "break;". If this is missing, the following case-commands are executed until a break-statement is reached. "break" causes the program to exit the switch-structure.


// Tu8.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class  Tu8
{
  Turtle joe = new Turtle();

  public Tu8()
  {
    joe.fillToPoint(0, 0);   
    for (int = 0; k < 6; k++ )
    {
      switch(k)
      {
        case 0:         
          joe.setPenColor(Color.red);
          break;
        case 1:
          joe.setPenColor(Color.yellow);
          break;
        case 2:
          joe.setPenColor(Color.blue);
          break;
        case 3:
          joe.setPenColor(Color.green);
          break;
        default:
          joe.setPenColor(Color.black);
      }
      
      for (int = 0; i < 3; i++)
      {
        joe.forward(100);
        joe.right(120);
      }
      joe.right(60);
    }
  }

  public static void main(String[] args)
  {
    new Tu8();
  }
}