早教吧作业答案频道 -->其他-->
关于C#中一个方法的使用问题staticboolIsPrime(intnum)如果num是素数便返回true,不然返回false并且使用一个while循环,利用以上method来展示0到1000之间的素数原题:Assumethefollowingmethodisavailab
题目详情
关于C#中一个方法的使用问题
static bool IsPrime (int num)
如果num是素数便返回true,不然返回false
并且使用一个while循环,利用以上method来展示0到1000之间的素数
原题:
Assume the following method is available for use:
static bool IsPrime (int num)
IsPrime takes a number and returns true if the number is prime, otherwise it returns false. (A number is said to be prime if it is only divisible by 1 and by itself).
Write a
while loop to display every prime number between 0 and 1000 using the above method. (That is, do NOT implement IsPrime)
static bool IsPrime (int num)
如果num是素数便返回true,不然返回false
并且使用一个while循环,利用以上method来展示0到1000之间的素数
原题:
Assume the following method is available for use:
static bool IsPrime (int num)
IsPrime takes a number and returns true if the number is prime, otherwise it returns false. (A number is said to be prime if it is only divisible by 1 and by itself).
Write a
while loop to display every prime number between 0 and 1000 using the above method. (That is, do NOT implement IsPrime)
▼优质解答
答案和解析
//此处只计算正整数的质数
public static bool IsPrime(int num)
{
if(num<2)
{
return false;
}
if(num==2)
{
return true;
}
for(int i=2;i<num;i++)
{
if(num%i==0)//如果能整除 就返回false
{
return false;
}
}
return true;
}
//展示0-1000之间的素数
public void Main()
{
int n = 0;
while(true)
{
if(n>=1000)break;
if(IsPrime) Console.WriteLine(n);
}
}
看了 关于C#中一个方法的使用问题...的网友还看了以下:
连字成词(英语)l u o e b s r u s r e t o s w a e s r t e 2020-05-14 …
u,i,n,c,o,s可以拼成什么单词?还有,o,s,l,h,u,d还有,t,p.s.n.e.r, 2020-05-23 …
质量为m=60kg的人站在质量为M=100kg的小车上,一起以v=3m/s的速度在光滑水平地面上做 2020-06-02 …
两艘船A与B,在t=0时从港口O处同时以相同的速度v=10m/s分别向东、向南匀速前进,如图所示. 2020-06-12 …
英语高手给我想几个单词LOS分别以“L”“O”“S”“T”为开头的英文单词最好和“LOST”有点关 2020-06-12 …
我那还有两个题,能帮忙吗?1.温度为7℃的空气在1atm下,以u=2m/s的速度掠过一大平板,空气 2020-07-18 …
排列成正确的句子:me,I,go,excuse,haveto.排列成正确的单词:1.c,d,e,s, 2020-11-01 …
相对运动问题一辆邮车以u=10m/s的速度沿平直公路匀速行驶.在离此公路d=50m处有一邮递员,当他 2020-11-08 …
如图所示,装置的左边是足够长的光滑水平台面,一轻质弹簧左端固定;装置的中间是水平传送带,它与左右两边 2020-12-12 …
以L,O,S,E,R开头的英文单词组成一句浪漫的话, 2021-01-25 …