/************************* 會考題第八題:處理成績 **************************/ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.util.*; public class b8 extends JFrame //畫圖物件是JFrame的擴充 { Container c; Color bgcolor=Color.white; Color textcolor=Color.red; JButton rot0,rot1,rot2; JRadioButton chbt,enbt,mtbt; ButtonGroup radiogroup = new ButtonGroup(); imgZ1 Z1= new imgZ1(); radioact RA1=new radioact(); static String level_name[]={"90~100","80~89","70~79","60~69","0~59"}; static String class_name[]={"國文","英文","數學"}; int clspt=0;//設定科目為國文 int ready=0;//先設定還沒有讀檔 String name[]=new String [200]; double score[][]=new double [200][3];//設定最多兩百人 int level[][]=new int[5][3];//三科,五個等級 int std_cnt=0;//學生計數器 public b8() //建構元 { super("轉換ASCII圖形"); c=getContentPane(); rot1=new JButton("開檔"); rot2=new JButton("顯示"); rot0=new JButton("離開"); chbt = new JRadioButton("國文",true); enbt = new JRadioButton("英文"); mtbt = new JRadioButton("數學"); radiogroup.add(chbt); radiogroup.add(enbt); radiogroup.add(mtbt); setSize(640,600); c.setLayout(new FlowLayout(FlowLayout.CENTER)); c.add(rot1); c.add(chbt); c.add(enbt); c.add(mtbt); c.add(rot2); c.add(rot0); //安裝listener rot0.addActionListener(Z1); rot1.addActionListener(Z1); rot2.addActionListener(Z1); chbt.addItemListener(RA1); enbt.addItemListener(RA1); mtbt.addItemListener(RA1); show(); } public void paint(Graphics g) //真正的畫圖設定 { int i,x,y; super.paint(g); g.setFont(new Font("Serif",Font.PLAIN,20)); if (ready==0) //還沒有讀檔 g.drawString("尚無資料!",5,100); else { //show 表 g.drawString("學生成績統計表:",5,100); g.drawString("分數",20,140); g.drawString("人數",100,140); for (i=0;i<5;i=i+1) { g.drawString(level_name[i],20,160+i*25); g.drawString(""+level[i][clspt],100,160+i*25); } //畫統計圖 g.drawString(class_name[clspt]+"成績統計表:",5,300); g.setColor(Color.black); g.drawLine(20,320,20,520); g.drawLine(20,520,400,520);//座標軸 g.setColor(Color.blue); //公式 520- cnt * 2 for (i=0;i<5;i=i+1) { x=40+i*70; y=520-level[i][clspt]*2; g.fillRect(x,y,40,level[i][clspt]*2); g.drawString(level_name[i],x,550); } } } /************選擇科目*************************/ class radioact implements ItemListener { public void itemStateChanged(ItemEvent e) { if (e.getSource() == chbt)//國文 clspt = 0; else if (e.getSource() == enbt) //英文 clspt = 1; else //數學 clspt = 2; //repaint(); } }//radioact class imgZ1 implements ActionListener { public void file_read() throws IOException //讀取成績檔,順便算成績 { int i,j; StringTokenizer stk; String getfr; BufferedReader fr= new BufferedReader(new FileReader("score"));//開檔 getfr = fr.readLine(); //取得一行輸入,跳過表頭 //初始化level陣列 for (i=0;i<3;i=i+1) for (j=0;j<5;j=j+1) level[j][i]=0; i=0; while (fr.ready())//如果檔案沒有讀完,就繼續處理 { getfr = fr.readLine(); //取得一行輸入 stk=new StringTokenizer(getfr," \n\t"); //取得token try{ name[i]=stk.nextToken(); for (j=0;j<3;j=j+1) {//取得成績,計算分佈狀況 score[i][j]=Double.parseDouble(stk.nextToken()); if (score[i][j]>=90) level[0][j]++; else if (score[i][j]>=80) level[1][j]++; else if (score[i][j]>=70) level[2][j]++; else if (score[i][j]>=60) level[3][j]++; else if (score[i][j]>=0) level[4][j]++; else System.out.println(score[i][j]+" 這個成績有問題!"); } i=i+1; } catch(NumberFormatException e){ //處理輸入文字 System.out.println(getfr+" 這個成績格式有問題!"); continue; } } fr.close(); } public void actionPerformed(ActionEvent e) { if (e.getSource()==rot0)//結束 { System.exit(0); } /***透過檢查那個按鈕呼叫的,就可以判定該做的動作 ***/ if (e.getSource()==rot1)//開檔 { try {file_read();} catch(IOException e1) { System.out.println("score Reading Error!"); } ready=1; } if (e.getSource()==rot2)//顯示 { repaint(); //重新顯示一次 } } //actionPerformed }//class public static void main(String args[]) //程式起點 { b8 app=new b8(); //啟動物件 app.addWindowListener(new WindowAdapter(){ //匿名內部類別 public void windowClosing(WindowEvent e) { System.exit(0); } }); //處理視窗關閉要求 } }