【问题标题】:Code to find indexes of array that are in prime number position and replace the array element with -1 in Java查找位于素数位置的数组索引并将数组元素替换为 Java 中的 -1 的代码
【发布时间】:2017-11-11 04:31:31
【问题描述】:

我是编程新手,我有一个任务要做,但我很挣扎,我不确定我做错了什么。我尝试了许多不同的方法并进行了修改以尝试使其正常工作,但我就是想不通。

我有一个 200 个数组,每个数组都使用 Arrays.fill 填充数字 10,我需要拥有它,以便将素数位置的所有数字替换为 -1 并打印出数组.例如10 -1 -1 10 -1 10...等

这就是我现在拥有的-

public static void main(String[] args) {

    int[] test = new int[200];
    Arrays.fill(test, 10);

    print(numbers);
    System.out.println();
    print(primeReplace(test));


}

public static void print(int[] a) {
    for(int i = 0; i < a.length; i++) {
        System.out.print(a[i] + " ");
    }
}

public static int[] primeReplace(int[] a) {

    System.out.println("Prime: ");

    for(int i = 1; i < a.length; i++)
    {
            if(isPrime(i))
            {
                a[i] = -1;
            }
    }
    return a;

}

public static boolean isPrime(int b) {

    for(int i = 2; i < b; i++)
    {
        if (b % i == 0) 
            return false;
    }
    return true;
}

返回 -1 -1 -1 -1 10 -1 10 -1 10 10 10 -1 10 -1 10 10 10 -1 10... 等。

我也试过这个 -

public static int[] primeReplace(int[] a) {

    System.out.println("Prime: ");

    for(int i = 0; i < a.length; i++)
    {
        for(int j = 2; j < i; j++)
        {
            if(i % j == 0)
            {
                a[i] = -1;
            }
        }
    }
    return a;
}

返回:10 10 10 10 -1 10 -1 10 -1 -1 -1 10 -1 10... etc.

我可能错过了一些非常简单的东西,因为我不擅长数学,但这让我发疯,我只是不明白,所以任何帮助将不胜感激。

【问题讨论】:

  • 10 -1 -1 10 -1 10 ??数组索引以 0 开头。
  • 提示:质数是一个大于一的数,它可以被任何除1或它本身以外的数整除。
  • 除了 print(numbers) 看不出代码有什么问题...我认为 primeReplace 上的 for 应该从 0 开始,在 isPrime 中 if 应该是 if (b % i == 0 &amp;&amp; b !=0) @ 987654321@
  • @JorgeCampos - 两者都不是! en.wikipedia.org/wiki/Prime_numberprimes.utm.edu/notes/faq/one.html。 Chloe13 的 isPrime 实现为 1 返回 true。
  • @StephenC 是的,你是对的,那么它应该是... &amp;&amp; b&gt;1 谢谢你指出:)

标签: java arrays for-loop if-statement methods


【解决方案1】:

使用下面的代码作为答案。

public class PrimeDemo {
public static void main(String[] args) {

    int[] test = new int[200];
    Arrays.fill(test, 10);

   // print(numbers);
    System.out.println();
    print(primeReplace(test));


}

public static void print(int[] a) {
    for(int i = 0; i < a.length; i++) {
        System.out.print(a[i] + " ");
    }
}

public static int[] primeReplace(int[] a) {

    System.out.println("Prime: ");

    for(int i = 0; i < a.length; i++)
    {
            if(isPrime(i))
            {
                a[i] = -1;
            }
    }
    return a;

}

private static boolean isPrime(int a) {
    if (a< 2) return false;
    if (a== 2) return true;
    if (a% 2 == 0) return false;
    for (int i = 3; i * i <= a; i += 2)
        if (a % i == 0) return false;
    return true;
}
}

我只是改变你的 isPrime 方法。素数总是从 2 开始,数组索引从 0 开始。

输出看起来像

素数: 10 10 -1 -1 10 -1 10 -1 10 10 10 -1 10 -1 10 10 10 -1 10 -1 10 10 10 -1 10 10 10 10 10 -1 10 -1 10 10 10 10 10 -1 10 10 10 -1 10 -1 10 10 10 -1 10 10 10 10 10 -1 10 10 10 10 10 -1 10 -1 10 10 10 10 10 -1 10 10 10 -1 10 -1 10 10 10 10 10 -1 10 10 10 -1 10 10 10 10 10 -1 10 10 10 10 10 10 10 -1 10 10 10 -1 10 -1 10 10 10 -1 10 -1 10 10 10 -1 10 10 10 10 10 10 10 10 10 10 10 10 10 -1 10 10 10 -1 10 10 10 10 10 -1 10 -1 10 10 10 10 10 10 10 10 10 -1 10 -1 10 10 10 10 10 -1 10 10 10 10 10 -1 10 10 10 -1 10 10 10 10 10 -1 10 10 10 10 10 -1 10 -1 10 10 10 10 10 10 10 10 10 -1 10 -1 10 10 10 -1 10 -1

【讨论】:

    【解决方案2】:

    使用这个方法:

    public boolean isPrimeNumber(int num) {
            int factors = 0;
            int j = 1;
    
            while(j <= num)
            {
                if(num % j == 0)
                {
                    factors++;
                }
                j++;
            }
            return (factors == 2);
      }
    

    或者这个方法:

    private static boolean isPrime(int num) {
            if (num < 2) return false;
            if (num == 2) return true;
            if (num % 2 == 0) return false;
            for (int i = 3; i * i <= num; i += 2)
                if (num % i == 0) return false;
            return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2016-06-09
      • 2019-10-12
      • 1970-01-01
      • 2022-06-19
      • 2019-01-12
      相关资源
      最近更新 更多