看到这题目,自己打了一遍代码,- class sushuDemo
- {
- public static void main(String[] args)
- {
- sushu s1=new sushu(1,200);//自己定义范围是1-200
- s1.ssmuch();
- }
- }
- class sushu
- {
- int[] sushu;
- int x,y;
- sushu(int x,int y)
- {
- this.x=x;
- this.y=y;
- }
- //素数是只能被自己和一整除的数
- public void ssmuch()
- {
- for(;x<=y;x++)
- {
-
- //这里如果为真,则输出该数
- if(panduan(x,y)) System.out.print(x+"\t");
- }
- }
- //这里是判断是否为素数的方法
- public boolean panduan(int temp,int max)
- {
- for(int i=2;i<=max;i++)
- {
- if(temp%i!=0)continue;
- else if(temp==2||i==temp)//2是特别的素数,所以把它加上和能被自己整除的数算上
- {continue;}
- return false;
- }return true;
- }
- }
复制代码
|