无聊的时候,做了今天老师课上要求练习编译个界面形式的小程序实现n!的算法; 1、用递归 2、用非递归 3、利用界面实现(小软件) import java.awt.*; import javax.swing.*; import java.awt.event.*; public class doIt extends JFrame{ private JPanel jp=new JPanel(); private JButton jb=new JButton("计算"); private JTextField jtext1=new JTextField(); private JTextField jtext2=new JTextField(); private JMenuBar mb=new JMenuBar(); private JMenu mm=new JMenu("File"); private JMenuItem mi1=new JMenuItem("Close"); private Label l=new Label("请输入n(1~19):"); private Label l1=new Label("输入:"); private Label l2=new Label("结果:"); int n; int total; String str; public doIt(){ jp.setLayout(null); l.setBounds(50,10,150,24); jp.add(l); l1.setBounds(10,50,40,24); jp.add(l1); jtext1.setBounds(60,50,100,22); jp.add(jtext1); l2.setBounds(10,90,40,24); jp.add(l2); jtext2.setBounds(60,90,100,22); jp.add(jtext2); jtext2.setEditable(false); jb.setBounds(50,120,70,26); jp.add(jb); mm.add(mi1); mb.add(mm); this.setJMenuBar(mb); this.add(jp); this.setBounds(300,150,200,300); this.setVisible(true); jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ str=jtext1.getText().trim(); n= Integer.parseInt(str); total=1; while(n>0){//核心计算n! total*=n--; } System.out.println(total); str=""+total; jtext2.setText(str); } }); } public static void main(String[] args) { // TODO Auto-generated method stub new doIt(); } }