2017佛山最低工资标准:JAVA编程:用一个按纽、一个标签、三个单选按钮,根据所选的单选按钮分别计算:1+2+、、、+n

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 09:06:31
用一个按纽、一个标签、三个单选按钮,根据所选的单选按钮分别计算: 1+2+、、、+n
1*2*、、、*n
1+1/2、、、+1/n
结果在标签中显示。

我强烈要求搂住给我追加分数,累死我了!

╰☆╮江水寒 版权所有
import java.awt.*;
import java.awt.event.*;

/**
* <p>Title: 图形用户界面</p>
*
* <p>Description: 简单的图形界面编程</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author ╰☆╮江水寒
* @version 1.0
*/
public class Baidu extends Frame implements ActionListener {
private TextField T;
private Button B1, B2, B3;
private Label L;
public Baidu() {
setSize(300, 150);
setBackground(Color.yellow);
WindowDestroyer Listener = new WindowDestroyer();
addWindowListener(Listener);
setTitle("三 种 功 能");
setLayout(new BorderLayout());
Panel p1 = new Panel();
p1.setBackground(Color.gray);
p1.setLayout(new FlowLayout());
L = new Label("请输入N:");
p1.add(L);
T = new TextField(10);
p1.add(T);
add(p1, BorderLayout.NORTH);
Panel p2 = new Panel();
p2.setBackground(Color.green);
p2.setLayout(new FlowLayout());
B1 = new Button("求和");
B1.setBackground(Color.red);
B1.addActionListener(this);
p2.add(B1);
B2 = new Button("求阶乘");
B2.setBackground(Color.red);
B2.addActionListener(this);
p2.add(B2);
B3 = new Button("求倒数和");
B3.setBackground(Color.red);
B3.addActionListener(this);
p2.add(B3);
add(p2, BorderLayout.CENTER);
setVisible(true);
}

public static void main(String[] args) {
new Baidu();
}

public void actionPerformed(ActionEvent e) { //事件处理方法
String s = e.getActionCommand();
int n = stringToInt(T.getText());
if (s.equals("求和")) {
T.setText(Integer.toString(sum(n)));
} else if (s.equals("求阶乘")) {
T.setText(Integer.toString(faction(n)));
} else if (s.equals("求倒数和")) {
T.setText(Double.toString(sumDaoshu(n)));
}
}

private static int sum(int n) { //求和
if (n == 1) {
return 1;
} else {
return sum(n - 1) + n;
}
}

private static int faction(int n) { //求阶乘
if (n == 1) {
return 1;
} else {
return faction(n - 1) * n;
}
}

private static double sumDaoshu(int n) { //求倒数和
if (n == 1) {
return 1;
} else {
return sumDaoshu(n - 1) + 1 / (double) n;
}
}

private static int stringToInt(String s) {
return Integer.parseInt(s.trim());
}

//关闭窗口监听类
private class WindowDestroyer extends WindowAdapter { //WindowAdapter是窗口适配器
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}

看到楼上的那么辛苦,我也就拱手相让了!