# 怎么限制列数输出？

yxq08nt 2010-03-13 09:55:16
public class Prime {
public static boolean isPrime(int num){
for (int i = 2; i <=Math.sqrt(num); i++) {
if ((num % i) == 0) {
return false;
}
}
return true;
}
public static void main(String[] args)
{
for(int i = 2; i <= 1000; i++)
{
if(isPrime(i))
{
System.out.print(i + " ");
}
}
}
}

...全文
89 6 打赏 收藏 转发到动态 举报

6 条回复

yxq08nt 2010-03-13
• 打赏
• 举报

[Quote=引用 5 楼 asiatju13 的回复:]

public static void main(String[] args) {
int count = 0;
for (int i = 2; i <= 1000; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
count++;
if (count == 5) {
……
[/Quote]

Asiatju13 2010-03-13
• 打赏
• 举报

public static void main(String[] args) {
int count = 0;
for (int i = 2; i <= 1000; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
count++;
if (count == 5) {
System.out.println();
count = 0;
}
}
}
}
yxq08nt 2010-03-13
• 打赏
• 举报

[Quote=引用 1 楼 rumlee 的回复:]
Java code

public class Prime {
public static boolean isPrime(int num){
for (int i = 2; i <=Math.sqrt(num); i++) {
if ((num % i) == 0) {
return false;
}
}
re……
[/Quote]

warrior30 2010-03-13
• 打赏
• 举报

``````

for (int i=0,j=0;i<1000;i++) {
if( j++<10 ){
System.out.print(i);
}
else {
j=0;
System.out.println();
}
}
``````

musiclee 2010-03-13
• 打赏
• 举报

rumlee 2010-03-13
• 打赏
• 举报

``````
public class Prime {
public static boolean isPrime(int num){
for (int i = 2; i <=Math.sqrt(num); i++) {
if ((num % i) == 0) {
return false;
}
}
return true;
}

public static void main(String[] args){
int n = 10;
int m = 0;
for(int i = 2; i <= 1000; i++) {

if(isPrime(i)){
System.out.print(i + " ");
if((++m)%n==0){
System.out.println();
}
}
}
}
}

``````

62,569

• 近7日
• 近30日
• 至今