public class text5 { private static int j = 1, counter = 0; public static void main(String[] args) { System.out.println("The following nums is the prime nums:"); for (int i = 1; i <= 100; i++) { while ( (j == 1) & (j <= Math.sqrt(i))) { j++; } if (i % j == 0) { System.out.println(i + "\t"); counter++; } if (counter % 5 == 0) { System.out.println("\r"); } } } } 如果不要换行,注释掉if(counter%5 ==0)
i want to know what's wrong with my program. who can tell me?
因为你生搬硬套数学结论,1、2何需放入代码测试,另外你的段子都没法帮你改。我重写了一个,很简单的:public class Test{ public static void main(String[] args){ for (int i = 3,j;i<=100;i++){ for(j = 2;j<i;j++){ if(i%j == 0)break; } if(i==j)System.out.print(i+"\t"); } } }如果问题解决,希望你能结贴,谢谢。
private static int j = 1, counter = 0;
public static void main(String[] args) {
System.out.println("The following nums is the prime nums:");
for (int i = 1; i <= 100; i++) {
while ( (j == 1) & (j <= Math.sqrt(i))) {
j++;
}
if (i % j == 0) {
System.out.println(i + "\t");
counter++;
}
if (counter % 5 == 0) {
System.out.println("\r");
}
}
}
}
如果不要换行,注释掉if(counter%5 ==0)
public static void main(String[] args){
for (int i = 3,j;i<=100;i++){
for(j = 2;j<i;j++){
if(i%j == 0)break;
}
if(i==j)System.out.print(i+"\t");
}
}
}如果问题解决,希望你能结贴,谢谢。