目录
一:问题:
二:思路分析:
三:具体代码:
四:运行结果:
一:问题:
二:思路分析:
三:具体代码:
Ⅰ:(双层循环)
package 重修;import java.util.Random;
import java.util.Scanner;public class first {public static void main(String[] args) {int count=0;for (int i = 101; i < 200; i++) {Boolean flag=true;for (int j = 2; j < i/2; j++) {if(i%j==0){flag=false;break;}}if(flag==true){System.out.println(i+"是素数");count++;}}System.out.println("共有"+count+"个素数");}
}
Ⅱ:(continue跳转量)
package 重修;import java.util.Random;
import java.util.Scanner;public class first {public static void main(String[] args) {int count=0;out:for (int i = 101; i < 200; i++) {Boolean flag=true;for (int j = 2; j < i/2; j++) {if(i%j==0){continue out;}}System.out.println(i+"是素数");count++;}System.out.println("共有"+count+"个素数");}
}
Ⅲ:(利用方法)
package 重修;import java.util.Random;
import java.util.Scanner;public class first {public static void main(String[] args) {int count=0;Boolean flag;for (int i = 101; i < 200; i++) {flag=panduan(i);if(flag==true){count++;System.out.println(i+"是素数");}}System.out.println("共有"+count+"个素数");}private static Boolean panduan(int i) {for (int j = 2; j < i/2; j++) {if(i%j==0){return false;}}return true;}
}