注意:刘征老师的课程考察,请青大的别抄袭,谢谢!

打印到一个html中,打开网页查看 ,网页在同一工程目录下。

package java_6_6;

public class admin {

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

	}
}
package java_6_6;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class java_6_6 extends JFrame {
	private JPanel contpane;
	private JTextField jf;
	private JButton jb;
	private String s;
	private String[] str;
	private int n;
	private int a[];
	private int cnt=0;
	private String ww="";
	public void init() {
		contpane = new JPanel();
		contpane.setLayout(null);
		jf = new JTextField();
		jb = new JButton("启动计算");
		contpane.add(jf);
		jf.setBounds(50, 20, 400, 30);
		contpane.add(jb);
		jb.setBounds(50, 55, 400, 50);
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					cnt=0;
					ww=" ";
					jisuan();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
	}
	private void dfs(String ss[],int sum,int l,int count,String w) throws IOException {
		if(count==l-1) {
			if(sum!=0) w+=ss[l-1]+sum;
			int ccc=1;
			String y = "";
			int fl=n;
			for (int i = 0; i < w.length();i++) {
				if(w.charAt(i)>='0'&&w.charAt(i)<='9') {
					y+=w.charAt(i);
				}
				else {
					if(y.length()!=0) {
						ccc*=jisuan(y,fl);
						fl-=Integer.valueOf(y);
						y="";
					}
				}
			}
			ww+=String.valueOf(ccc)+w+" ";
			//System.out.println(ww);
			return;
		}
		String sss=w;
		for (int i = n; i >= 0;i--) {
			if(i<=sum) {
				if(i!=0) w+=ss[count]+i;
				dfs(ss,sum-i,l,count+1,w);			
			}
			w=sss;
		}
	}
	private int jisuan(String y,int fl) {
		int nn=fl;
		int w=Integer.valueOf(y);
		int fz=1;
		int fm=1;
		for (int i = nn-w+1; i <= nn;i++) {
			fz*=i;
		}
		for (int j = 1; j <= w;j++) {
			fm*=j;
		}
		return fz/fm;
	}
	private void jisuan() throws IOException {
		s = jf.getText();
		str = s.split(" ");
		n = Integer.parseInt(str[str.length-1]);
		//System.out.println(n);
		a=new int[1000];
		int l=str.length-1;
		dfs(str,n,l,0,"");
		JOptionPane.showMessageDialog(this, "计算成功,请查看网页,网页在同一工程目录下!");
		print();
	}
	public void print() throws IOException {
		String sx=ww;
		String cc="";
		ww="";
		File fp = new File("lgz.html");
		try {
			BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fp)));
			fout.append("<html>");
			fout.append("<body>");
			fout.append("<p>");
			fout.append("(");
			for (int i = 0; i < str.length-1;i++) {
				if(i!=str.length-2) fout.append(str[i]+"+");
				else fout.append(str[i]);
			}
			fout.append(")");
			fout.append("<sup>"+String.valueOf(n)+"</sup>");
			fout.append("=");
			for (int i = 0; i < sx.length();i++) {
				if(sx.charAt(i)==' ') {
					if(i!=0&&i!=sx.length()-1) fout.append("+");
					cc="";
					for (int j = i+1; j < sx.length();j++) {
						if(sx.charAt(j)>='0'&&sx.charAt(j)<='9') {
							cc+=sx.charAt(j);
						}
						else {
							i=j-1;
							break;
						}
					}
					if(cc.length()==1&&Integer.valueOf(cc)==1) fout.append("");
					else fout.append(cc);
				}
				else if(sx.charAt(i)>='0'&&sx.charAt(i)<='9') {
					fout.append("<sup>");
					for (int j = i; j < sx.length();j++) {
						if(sx.charAt(j)>='0'&&sx.charAt(j)<='9') {
							ww+=sx.charAt(j);
						}
						else {
							i=j-1;
							break;
						}
					}
					if(ww.length()==1&&Integer.valueOf(ww)==1) fout.append("");
					else fout.append(ww);
					fout.append("</sup>");
				}
				else {
					ww="";
					fout.append(sx.charAt(i));
				}
			}
			fout.append("</p>");
			fout.append("</body>");
			fout.append("</html>");
			fout.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public java_6_6() {
		init();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
		setBounds(100,100,500,200);
		this.setTitle("计算");
		this.add(contpane);
		this.setVisible(true);
	}

}