这是一个自己输入范围的小程序,如果你为了完成特定的作业就把我的提示输入与接收删掉
代码很简单我就不多说啦自己看吧嘻嘻。
import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入起始数字(不要小于1呦!!!):"); int n1 = input.nextInt();//接收第一个数字 System.out.print("请输入结束数字:"); int n2 = input.nextInt();//接收第二个数字 System.out.println(n1 + "-" + n2 + "的质数有:"); if (n1 == 1) { n1 += 1; } int count = 0;//声明变量赋初值 for (int i = n1; i <= n2; i++) { Boolean demo = true;//定义个标志 for (int j = n1; j <= Math.sqrt(i); j++) { if (i % j == 0) { demo = false; break; } } if (demo) { count++;//进行计数 System.out.print(i + " "); } } System.out.println(); System.out.println("其中质数有" + count + "个"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)