- package cn.itcast;
- public class ZhiShu2 {
- public static void main(String[] args) {
- for (int i = 3; i <10; i++) {
- for (int j = 2; j <=(i+1)/2; j++) {
- if (i%j == 0) {
- break;
- }
- }
- if(true){
- System.out.println(i);
- }
- }
- }
- }
复制代码
这个代码中是缩小了除数的范围,,被1和本身之外的数整除,就会跳出循环,,,比着上个代码少了很多运算,,,思路同样很简单. |
|