随机生成60个1-100之间不同的整数,在控制台上打印出这些数字中能被3整除但不能被7整除的数字的程序
1-100之间被3整除但不能被7整除的数字只有29个,所以无法实现。
public class random {
public static void main(String[] args) {
random random = new random();
System.out.println(random.helper());
}
public ArrayList helper(){
ArrayList res = new ArrayList();
Map map = new HashMap<>();
int count = 0;
while (count int i = (int) (Math.random()*100+1);
if (!map.containsKey(i)){
map.put (i,null);
count++;
}
}
System.out.println(map.size());
for (Map.Entry cur:map.entrySet()){
if (cur.getKey()%3==0&&cur.getKey()%7!=0){
res.add(cur.getKey());
}
}
return res;
}
}
写的有点取巧,比较简单,但能实现效果
package com.caissa.test.testnum;
import java.util.Random;
public class TestRandomA {
public void getRandom(){
int b = 0;
for(int i =0; i<10000;i++){
Random random = new Random();
int a =random.nextInt(100);
if(a % 3 != 0){
//如果不能被3整除,跳出这次循环
continue;
}
if(a % 7 == 0){
//如果能被7整除,跳出这次循环
continue;
}
b = b+1;
if(b>60){
//如果a的个数超过60个跳出循环
break;
}
System.out.println(a);
}
}
public static void main(String[] args) {
TestRandomA b = new TestRandomA();
b.getRandom();
}
}