产生随机数时出现问题
运行后每次点小应用程序查看器 都会重新产生随机数,求解??程序代码:
package ClosestPoints; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class ClosestPoints extends Applet implements ActionListener { int i, j, a, b, n, A[][]; @Override public void paint(Graphics g) { // TODO Auto-generated method stub // super.paint(g); Random r = new Random(); n = Math.abs(r.nextInt(18)) + 2; // 随机产生点的个数(2~20) A = new int[n][2]; // 随机产生坐标 for (i = 0; i < n; i++) { A[i][0] = Math.abs(r.nextInt(1000)); A[i][1] = Math.abs(r.nextInt(1000)); System.out.println("A" + (i + 1) + "(" + A[i][0] + "," + A[i][1] + ")"); } // 描点 for (i = 0; i < n; i++) { g.setColor(Color.red); g.drawOval(A[i][0], A[i][1], 5, 5); g.fillOval(A[i][0], A[i][1], 6, 6); g.setColor(Color.blue); g.drawString("A" + (i + 1) + "(" + A[i][0] + "," + A[i][1] + ")", A[i][0] - 25, A[i][1] + 20); } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
[ 本帖最后由 天涯为客 于 2013-4-1 20:31 编辑 ]