Switch

The switch command is a replacement for a series of if…else statements.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Days extends Applet implements ActionListener
{

int day;
public TextField dayNumber;
public void init()
{

dayNumber = new TextField(1);
add (dayNumber);
dayNumber.addActionListener(this);

}
public void paint(Graphics g)
{

switch (day)
{

case 1:g.drawString("Sunday", 50, 50);break;
case 2:g.drawString("Monday", 50, 50);break;
case 3:g.drawString("Tuesday", 50, 50);break;
case 4:g.drawString("Wednesday", 50, 50);break;
case 5:g.drawString("Thursday", 50, 50);break;
case 6:g.drawString("Friday", 50, 50);break;
case 7:g.drawString("Saturday", 50, 50);break;
default:g.drawString("Not a week day", 50, 50);
break;

}

}
public void actionPerformed (ActionEvent event)
{

day=Integer.parseInt(dayNumber.getText());
repaint();

}

}

switch.JPG
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License