關於部落格
雨--是天空的眼淚 風--是蝴蝶的痴醉 雲--是水珠的嫵媚 光--是溫暖的慰藉 夜--是孤單的滋味 星--是墜跌的心碎 蹲下,是為了躍起。 後退,是為了向前跳得更遠。
  • 171475

    累積人氣

  • 4

    今日人氣

    0

    訂閱人氣

Java GPA計算(介面化)



沒提供原始檔
因為要的就自己複製吧
這樣才會自己看過一遍(縮排)


檔名:gpa.java

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;

public class gpa extends Frame
{
 JTextField CN_1 = new JTextField("");
 JTextField CN_2 = new JTextField("");
 JTextField CN_3 = new JTextField("");
 JTextField CN_4 = new JTextField("");
 JTextField S1 = new JTextField("");
 JTextField S2 = new JTextField("");
 JTextField S3 = new JTextField("");
 JTextField S4 = new JTextField("");
 JTextField C1 = new JTextField("");
 JTextField C2 = new JTextField("");
 JTextField C3 = new JTextField("");
 JTextField C4 = new JTextField("");

 JButton b1 = new JButton("Confirm");

 Label L_Course = new Label("Course Name",Label.CENTER);
 Label L_Score = new Label("Score",Label.CENTER);
 Label L_Credit = new Label("Credit",Label.CENTER);

 public gpa()
 {
  this.setTitle("學生成績單");
  this.setLayout(null);
  this.setResizable(false);
  this.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });

  CN_1.setBounds(25,80,80,20);
  CN_1.setHorizontalAlignment(JTextField.RIGHT);
  this.add(CN_1);
  CN_2.setBounds(25,105,80,20);
  CN_2.setHorizontalAlignment(JTextField.RIGHT);
  this.add(CN_2);
  CN_3.setBounds(25,130,80,20);
  CN_3.setHorizontalAlignment(JTextField.RIGHT);
  this.add(CN_3);
  CN_4.setBounds(25,155,80,20);
  CN_4.setHorizontalAlignment(JTextField.RIGHT);
  this.add(CN_4);
  S1.setBounds(125,80,80,20);
  S1.setHorizontalAlignment(JTextField.RIGHT);
  this.add(S1);
  S2.setBounds(125,105,80,20);
  S2.setHorizontalAlignment(JTextField.RIGHT);
  this.add(S2);
  S3.setBounds(125,130,80,20);
  S3.setHorizontalAlignment(JTextField.RIGHT);
  this.add(S3);
  S4.setBounds(125,155,80,20);
  S4.setHorizontalAlignment(JTextField.RIGHT);
  this.add(S4);
  C1.setBounds(225,80,80,20);
  C1.setHorizontalAlignment(JTextField.RIGHT);
  this.add(C1);
  C2.setBounds(225,105,80,20);
  C2.setHorizontalAlignment(JTextField.RIGHT);
  this.add(C2);
  C3.setBounds(225,130,80,20);
  C3.setHorizontalAlignment(JTextField.RIGHT);
  this.add(C3);
  C4.setBounds(225,155,80,20);
  C4.setHorizontalAlignment(JTextField.RIGHT);
  this.add(C4);

  b1.setBounds(0,217,330,30);
  b1.addActionListener(new myActionListener1());
  this.add(b1);

  L_Course.setBounds(25,55,80,20);
  L_Course.setBackground(Color.yellow);
  this.add(L_Course);
  L_Score.setBounds(125,55,80,20);
  L_Score.setBackground(Color.yellow);
  this.add(L_Score);
  L_Credit.setBounds(225,55,80,20);
  L_Credit.setBackground(Color.yellow);
  this.add(L_Credit);

  this.setBackground(new Color(197,191,255));
  setSize(330, 250);
  show();
 }

 public class myActionListener1 implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   String[] course_arr= {CN_1.getText().trim(),CN_2.getText().trim(),CN_3.getText().trim(),CN_4.getText().trim()};
   try
   {
    int i,Grade_Score=0;
    int Score_Sum=0,Credit_Sum=0,Sum=0;
    String Grade="";
    String rs_string="";
    Double AVG_rs,GPA_rs;
    int[] score_arr = {Integer.parseInt(S1.getText().trim()),Integer.parseInt(S2.getText().trim()),Integer.parseInt(S3.getText().trim()),Integer.parseInt(S4.getText().trim())};
    int[] credit_arr = {Integer.parseInt(C1.getText().trim()),Integer.parseInt(C2.getText().trim()),Integer.parseInt(C3.getText().trim()),Integer.parseInt(C4.getText().trim())};
    DecimalFormat round_off=new DecimalFormat("0.##");

    for(i=0;i<score_arr.length;i++){
     if(score_arr[i]>=90){
      Grade="A";
      Grade_Score=4;
     }
     else if(score_arr[i]>=75&&score_arr[i]<=89){
      Grade="B";
      Grade_Score=3;
     }
     else if(score_arr[i]>=65&&score_arr[i]<=74){
      Grade="C";
      Grade_Score=2;
     }
     else if(score_arr[i]>=50&&score_arr[i]<=64){
      Grade="D";
      Grade_Score=1;
     }
     else if(score_arr[i]<=49){
      Grade="F";
      Grade_Score=0;
     }
     Score_Sum+=score_arr[i]*credit_arr[i]; //各科分數 * 各學分
     Credit_Sum+=credit_arr[i]; //計算總學分
     Sum+=Grade_Score*credit_arr[i]; //Grade_Score * 各學分
     rs_string+=course_arr[i]+" : "+Grade+"rn";
    }
    AVG_rs=(Score_Sum+0.0)/(Credit_Sum+0.0);
    GPA_rs=(Sum+0.0)/(Credit_Sum+0.0);
    rs_string+="平均 : "+String.valueOf(round_off.format(AVG_rs))+"rn";
    rs_string+="GPA : "+String.valueOf(round_off.format(GPA_rs))+"rn";
    JOptionPane.showMessageDialog(null,rs_string,"學生成績單",JOptionPane.INFORMATION_MESSAGE);
   }
   catch(NumberFormatException ecp)
   {
    JOptionPane.showMessageDialog(null,"必須輸入整數值 or 欄位不可空白");
   }
  }
 }

 public static void main(String[] para)
 {
  new gpa();
 }
}



文:葉小誠

相簿設定
標籤設定
相簿狀態