问一下本人划下来的1号段子你们有没有中断的情况?就是书上1号段子写的情况!假如有这个情况,你们认为出这本书的老师写的对吗?谢谢
请看代码(求素数):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch11Ex03
{
public class Primes
{
private long min;
private long max;
public Primes()
: this(2, 100)
{
}
public Primes(long minimum, long maximum)
{
if (minimum < 2)
min = 2;
else
min = minimum;
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
class Program
{
static void Main(string[] args)
{
Primes primesFrom2To1000 = new Primes(2,1000);
foreach (long i in primesFrom2To1000)
Console.Write(“{0} “, i);
Console.ReadKey();
}
}
}
请看教材示例说明:
请看代码(求素数):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch11Ex03
{
public class Primes
{
private long min;
private long max;
public Primes()
: this(2, 100)
{
}
public Primes(long minimum, long maximum)
{
if (minimum < 2)
min = 2;
else
min = minimum;
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
class Program
{
static void Main(string[] args)
{
Primes primesFrom2To1000 = new Primes(2,1000);
foreach (long i in primesFrom2To1000)
Console.Write(“{0} “, i);
Console.ReadKey();
}
}
}
请看教材示例说明:
解决方案
20
而是一遍打印一遍找结果的 –> 而是一边打印一边找结果的
打错一个字,意思就错了哇。
打错一个字,意思就错了哇。