循环有问题,首先你的count定义下外面,count的值一直在改变,当i=2的时候,2%2的时候,count已经变为3了,所以不会再有了下面是正确的代码- public static void main(String[] args) {
-
- ArrayList<Integer>al=new ArrayList<Integer>();
- for(int x=1;x<=100;x++){
- int count=0;
- for(int y=1;y<=x;y++){
-
- if(x%y==0){
- count++;
- }
- }
- if(count<=2){
- al.add(x);
- }
- }
- System.out.println(al);
- }
- }
复制代码 |