开始
输入成绩:平时成绩,期中成绩,期末成绩,其它成绩
判断平时成绩,期中成绩,期末成绩在0到100之间,其他成绩在0到10之间
计算
输出
这样就行了吧。
下面呢就是示例代码了。墙头草
package me.shage.eleven.first;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import me.shage.eleven.first.ElevenInventory.Btn_Click;
public class Results extends JFrame{
private JTextField txtUsually;
private JTextField txtInterim;
private JTextField txtFinal;
private JTextField txtOther;
private JTextField txtSum;
private JButton btnCal;
public Results()
{
this.setLayout(null);
JLabel lbU=new JLabel("平时成绩");
lbU.setBounds(20, 10, 60, 21);
this.add(lbU);
txtUsually=new JTextField("0");
txtUsually.setBounds(80, 10, 25, 21);
this.add(txtUsually);
JLabel lbI=new JLabel("期中成绩");
lbI.setBounds(110, 10, 60, 21);
this.add(lbI);
txtInterim=new JTextField("0");
txtInterim.setBounds(170, 10, 25, 21);
this.add(txtInterim);
JLabel lbF=new JLabel("期末成绩");
lbF.setBounds(200, 10, 60, 21);
this.add(lbF);
txtFinal=new JTextField("0");
txtFinal.setBounds(260, 10, 25, 21);
this.add(txtFinal);
JLabel lbO=new JLabel("其它成绩");
lbO.setBounds(290, 10, 60, 21);
this.add(lbO);
txtOther=new JTextField("0");
txtOther.setBounds(350, 10, 25, 21);
this.add(txtOther);
JLabel lbS=new JLabel("总成绩");
lbS.setBounds(20, 35, 60, 21);
this.add(lbS);
txtSum=new JTextField("0");
txtSum.setBounds(80, 35, 40, 21);
this.add(txtSum);
btnCal=new JButton("计算");
btnCal.setBounds(300, 35, 60, 21);
this.add(btnCal);
btnCal.addActionListener(new Btn_Click());
this.setSize(420, 112);
this.setVisible(true);
}
class Btn_Click implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double sumResults;
double usuallyResults;
double interimResults;
double finalResults;
double otherResults;
usuallyResults=Double.valueOf(txtUsually.getText());
interimResults=Double.valueOf(txtInterim.getText());
finalResults=Double.valueOf(txtFinal.getText());
otherResults=Double.valueOf(txtOther.getText());
if(usuallyResults>100||usuallyResults<0)
{
JOptionPane.showMessageDialog(null, "平时成绩无效,请重新输入有效分数!");
}
else if(interimResults>100||interimResults<0)
{
JOptionPane.showMessageDialog(null, "期中成绩无效,请重新输入有效分数!");
}
else if(finalResults>100||finalResults<0)
{
JOptionPane.showMessageDialog(null, "期末成绩无效,请重新输入有效分数!");
}
else if(otherResults>10||otherResults<0)
{
JOptionPane.showMessageDialog(null, "其它成绩不能大于10分,请重新输入有效分数!");
}
else
{
sumResults=usuallyResults*0.3+interimResults*0.3+finalResults*0.4+otherResults;
DecimalFormat sum = new DecimalFormat("0.0");//保留一位小数
txtSum.setText(String.valueOf(sum.format(sumResults)));
}
}
}
}