标签:控制 技术分享 path 作业 控制流 ret 分享 nod ==
printPrimes():
public static String printPrimes(int n){
int max=100;
int curPrime;
int numPrimes;
boolean isPrime;
String result = "";
int [] primes = new int [max];
primes[0] = 2;
numPrimes = 1;
curPrime = 2;
while (numPrimes < n){
curPrime++;
isPrime = true;
for (int i = 0; i <= numPrimes-1; i++)
{
if (curPrime%primes[i]==0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
primes[numPrimes] = curPrime;
numPrimes++;
}
}
for (int i = 0; i <= numPrimes-1; i++) {
result += primes[i]+" ";
}
return result;
}
控制流图:
覆盖:
Node coverage:{1、2、3、4、5、6、7、8、9、10、11、12}
Edge coverage:{ (1,2),(1,8),(2,3),(3,4),(3,5),(4,5),(4,6),(5,7),(5,8),(6,3),(7,1),(8,9),(9,10),(9,12),(10,11),(11,9) }
Prime path coverage:{(1,2,3,4,6),(1,2,3,5,7),(1,8,9,10,11),(1,8,9,12)}
程序:
Main.java
package primes; public class Main { /*public static void main(String[] args){ System.out.println(printPrimes(20)); }*/ public static String printPrimes(int n){ int max=100; int curPrime; int numPrimes; boolean isPrime; String result = ""; int [] primes = new int [max]; primes[0] = 2; numPrimes = 1; curPrime = 2; while (numPrimes < n){ curPrime++; isPrime = true; for (int i = 0; i <= numPrimes-1; i++) { if (curPrime%primes[i]==0) { isPrime = false; break; } } if (isPrime) { primes[numPrimes] = curPrime; numPrimes++; } } for (int i = 0; i <= numPrimes-1; i++) { result += primes[i]+" "; } return result; } }
Maintest.java
package primes; import static org.junit.Assert.assertEquals; import org.junit.Test; public class Maintest { String result; @Test public void test1(){ result = Main.printPrimes(10); assertEquals("2 3 5 7 11 13 17 19 23 29 ",result); } @Test public void test2(){ result = Main.printPrimes(20); assertEquals("2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 ",result); } }
结果:
标签:控制 技术分享 path 作业 控制流 ret 分享 nod ==
原文地址:http://www.cnblogs.com/lxtao/p/6551304.html