java中的SWITCH语句的应用

package aad;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * <p>Title: switch sentence</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Frame1 extends JFrame {
  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();
  TextArea textArea1 = new TextArea();

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();

 

    textArea1.append("\n first situation:\n");
    char grade='C';
    switch(grade){
    case'A':textArea1.append(grade+"is 85~100\n");
  break;
case'B':textArea1.append(grade+"is 70~84\n");
  break;
case'C':textArea1.append(grade+"is 60~69\n");
break;
case'D':textArea1.append(grade+"is <60\n");
break;
  default:textArea1.append("input error\n");
    }

    textArea1.append("\nsecond situation:\n");
    grade='B';
    switch(grade){
    case'A':textArea1.append(grade+"is 85~100\n");
  case'B':textArea1.append(grade+"is 70~84\n");
  case'C':textArea1.append(grade+"is 60~69\n");
  case'D':textArea1.append(grade+"is <60\n");
    textArea1.append("\nsecond situation:\n");
  }
  textArea1.append("\n third sotiatopm:\n");
  grade='B';
  switch(grade){
  case'A':
case'B':
case'C':textArea1.append(grade+"is>=60\n");
break;
case'D':textArea1.append(grade+"is<60\n");
break;
    default:textArea1.append(grade+"input error\n");
}
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    contentPane.add(textArea1, BorderLayout.NORTH);
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
}

posted @ 2006-04-08 18:03  Tnu  阅读(5974)  评论(2编辑  收藏  举报