public class sushu {
public static void main(String[] args) {
System.out.print("100以内的素数是:"); outer: for (int i = 3; i < 101; i++) {
boolean K = true;
for (int y = 2; y < i; y++) {
if (i % y == 0)
K = false;
break;
}
if (K == false)
continue outer;
if (K == true)
System.out.print(i + " ");
}
}
}输出时00以内的素数是:3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 ,很郁闷为什么能输出9之类的数来的?查了好多遍还是找不出原因,大大们帮帮
public static void main(String[] args) {
System.out.print("100以内的素数是:"); outer: for (int i = 3; i < 101; i++) {
boolean K = true;
for (int y = 2; y < i; y++) {
if (i % y == 0)
K = false;
break;
}
if (K == false)
continue outer;
if (K == true)
System.out.print(i + " ");
}
}
}输出时00以内的素数是:3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 ,很郁闷为什么能输出9之类的数来的?查了好多遍还是找不出原因,大大们帮帮
if (i % y == 0)
K = false;
break;
}
里面只执行了一遍,你要用{}把break放进去啊。晕死了。if里面就算只有一句话,也要把它用{}括起来增加可读性,并且以后要加一句就不用在弄{}了。
还有你怎么用了C里面的goto???
完全可以if(!K){...}else{...}么。。
for (int i = 2; i < num; i++) {//运行效率不高
if ((num % i) == 0) {
return false;
}
}
return true;
}
public static void main(String[] args){
for(int i = 2; i <= 100; i++) {
if(isPrime(i)){
System.out.print(i + " ");
}
}
}
{
static void Prime()
{
int num, i, j = 0;
bool flag = true;
for (num = 1; num <= 999; num++)
{
for (i = 2; i <= (int)Math.Sqrt(num); i++)
{
if (num % i == 0)
{ flag = false; break; }
}
if (flag)
{
Console.Write("{0,-5}", num);
j++;
if (j % 10 == 0) Console.WriteLine();
}
flag = true;
}
}
static void Main(string[] args)
{
Prime();
Console.ReadKey();
}
}