import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Random;public class Rd { static Map setRd(int n) { Random out = new Random(System.currentTimeMillis()); Map contain = new HashMap(); while (n > 0) { int x = out.nextInt(10); int y = out.nextInt(10); if (!contain.containsValue("(" + x + "," + y + ")")) contain.put(n--, "(" + x + "," + y + ")"); } return contain; } public static void main(String[] args) throws IOException { System.out.println("enter the numbers of your coordinate:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int c = Integer.parseInt(s); Map result = Rd.setRd(c); for (int a = 1; a < result.size() + 1; a++) { System.out.print(" " + result.get(a)); } } }
随机就用1楼的办法.
private int x;
private int y;
}然后定义一个类来随即生成不重复坐标public class PointGenerator {
Set<Point> generatedPoint;//记录已经生成的point
static void generateNextPoint(){
//用java.util.Random中的 nextInt(int n) //n 为你的坐标允许的最大值
//生成一个一个point
// while (point 在GeneratedPoint中){
//生成下一个随机point
//把point 放入 generatedPoint
//break;
}
}
}
当然必须重写 equal 和 hashcode,这样才能正确判断一个点是不是在generatedPoint中
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class Rd {
static Map setRd(int n) {
Random out = new Random(System.currentTimeMillis());
Map contain = new HashMap();
while (n > 0) {
int x = out.nextInt(10);
int y = out.nextInt(10);
if (!contain.containsValue("(" + x + "," + y + ")"))
contain.put(n--, "(" + x + "," + y + ")");
} return contain;
} public static void main(String[] args) throws IOException {
System.out.println("enter the numbers of your coordinate:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int c = Integer.parseInt(s);
Map result = Rd.setRd(c);
for (int a = 1; a < result.size() + 1; a++) {
System.out.print(" " + result.get(a));
}
}
}