import java.awt.*; import java.applet.*; import java.awt.event.*; public class myclass extends Applet implements ActionListener {Label l1,l2,l3; TextField t1,t2,t3; int a[]; float avg; public void init() {l1=new Label("请输入个数:"); t1=new TextField(6); l1=new Label("输入范围from"); t2=new TextField(6); l3=new Label("to"); t3=new TextField(6); add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); t3.addActionListener(this); } public void paint(Graphics g) { int i; for(i=0;i<a.length;i++) g.drawString(a[i],15,i*20); g.drawString("平均数为:"+avg,50,20); }
public void actionPerformed(ActionEvent e) {int n,from,to,i,j,t; n=Integer.parseInt(t1.getText()); from=Integer.parseInt(t2.getText()); to=Integer.parseInt(t3.getText()); to-=from; a=new int[n]; for(i=0;i<a.length;i++) {a[i]=(int)(Math.random()*to)+from; avg+=a[i];} avg/=n; for(i=0;i<a.length-1;i++) {for(j=i+1;j<a.length;j++) if(a[i]>a[j]) {t=a[i];a[i]=a[j];a[j]=t;} repaint(); } } 找不出错在哪?
[此贴子已经被作者于2005-4-26 13:48:49编辑过]