简体   繁体   English

Java 数组查找素数的问题

[英]Problem with Java Array finding prime numbers

Here is the code这是代码

import java.util.Scanner;

public class oneDimensionArray {
    public static void main (String[] args) {
        finding(2,100);
    }
    public static void finding(int start, int end) {
        int[] primeNumbers = new int[end];

        for (int n = start; n < end; n++) {
            boolean check = true;
            int tryNum = 2;
            while (tryNum <= n / 2) {
                if (n % tryNum == 0) {
                    check = false;
                    break;
                }
                tryNum += 1;
            }
            if (check) {
                primeNumbers[n] = n;
            }
        }

        for (int i : primeNumbers) {
            System.out.print(i + ", ");
        }
    }
}

The result successfully shows all prime numbers, but there are a lot of zeros substituting the non-prime numbers, just like this: 0, 0, 2, 3, 0, 5, 0, 7, 0, 0, 0, 11, 0, 13, 0, 0, 0, 17, 0, 19, 0, 0, 0, 23, 0, 0, 0, 0, 0, 29, 0, 31, 0, 0, 0, 0, 0, 37, 0, 0, 0, 41, 0, 43结果成功显示了所有质数,但是有很多零代替了非质数,就像这样:0, 0, 2, 3, 0, 5, 0, 7, 0, 0, 0, 11, 0, 13, 0, 0, 0, 17, 0, 19, 0, 0, 0, 23, 0, 0, 0, 0, 0, 29, 0, 31, 0, 0, 0, 0, 0, 37, 0, 0, 0, 41, 0, 43

I wonder where is the problem with my code, and how everything is caused.我想知道我的代码哪里有问题,一切都是怎么造成的。

Why you take new array it's allocate memory space...为什么要使用新数组分配 memory 空间...

public class MyClass {
   public static void main (String[] args) {
        finding(100);
    }
    
    public static void finding(int end) {
    int count = 0;
    for (int n = 2; n < end; n++) {
        count = 0;
        int tryNum = 1;
        while (tryNum <= n) {
            if (n % tryNum == 0) {
                count++;
            }
            tryNum++;
        }
        
        if(count==2)
           System.out.print(n+"  ");     
        }
    }
}

You should add a variable to store the number of primes found and do this.您应该添加一个变量来存储找到的素数并执行此操作。

public static void finding(int start, int end) {
    int[] primeNumbers = new int[end];

    int count = 0;
    for (int n = start; n < end; n++) {
        boolean check = true;
        int tryNum = 2;
        while (tryNum <= n / 2) {
            if (n % tryNum == 0) {
                check = false;
                break;
            }
            tryNum += 1;
        }
        if (check) {
            primeNumbers[count++] = n;
        }
    }

    for (int i = 0; i < count; ++i) {
        System.out.print(primeNumbers[i] + ", ");
    }
}

output: output:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 

In java, every cell in an int array is initialled to 0, so when you print the array the output is indeed, bunch of zeros.在 java 中,int 数组中的每个单元格都初始化为 0,因此当您打印数组时,output 确实是一堆零。 to fix it, you can use a simple if statement to check of the value of a cell is not 0 like that:要修复它,您可以使用一个简单的 if 语句来检查单元格的值不是 0 像这样:

for (int i : primeNumbers) {
   if (i != 0) {
     System.out.println(i + ", ");
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM