【问题标题】:isPrime returns different result for the same value?isPrime 为相同的值返回不同的结果?
【发布时间】:2019-10-31 14:17:35
【问题描述】:

这是一个程序,用于确定 10 个数字数组中的元素是否为素数。主要的将被 -1 替换,其他的在打印时保持不变。

这对我来说似乎很好,但是当我使用 9 运行我的代码时,有些会得到 -1,这意味着 9 是素数(错误),有些返回 9(不是素数)。为什么我会遇到这种情况?有人可以帮忙吗

import java.util.Scanner;

public class Question5{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int[] array = new int [10];

        for (int i = 0; i < array.length; i++){
            System.out.println("Enter your number " + i);
            array[i] = input.nextInt();
        }

        System.out.println("Before method: ");
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();

        prime(array);
        System.out.println("After the method: ");
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();


    }

    public static void prime(int[] list){
        boolean isPrime = true;
        int count = 0;

        for (int i = 0; i < list.length; i++){
            isPrime = true;
            for (int j = 2; j < i; j++){
                count = i;
                if (list[i] % j == 0){
                    isPrime = false;
                    break;
                }
                if (list [i] == 0 || list[i] == 1){
                    isPrime = false;
                    break;
                }
                if (list [i] == 2){
                    isPrime = true;
                    list[count] = -1;
                }
            }
            if (isPrime){
                list[count] = -1;
            }

        }
    }
}

【问题讨论】:

  • 我认为你的第二个for循环应该是j &lt; list[i]

标签: java arrays primes


【解决方案1】:

你有三个错误:

  1. 您正在更新 list[count] 而不是 list[i]
  2. 内循环条件错误。应该是j &lt; list[i]j *j &lt;= list[i]
  3. 您的代码将01 识别为素数,这是不正确的。您应该在内部循环之前测试if (list [i] == 0 || list[i] == 1)

代码应如下所示:

public static void prime(int[] list){
    boolean isPrime = true;
    int count = 0;

    for (int i = 0; i < list.length; i++){
        isPrime = true;
        if (i < 2) {
          isPrime = false;
        } else {
            for (int j = 2; j * j <= list[i]; j++){
                count = i;
                if (list[i] % j == 0){
                    isPrime = false;
                    break;
                }
                if (list [i] == 2){
                    isPrime = true;
                    list[i] = -1;
                }
            }
        }
        if (isPrime){
            list[i] = -1;
        }

    }
}

例如:

Enter your number 0
0
Enter your number 1
1
Enter your number 2
2
Enter your number 3
3
Enter your number 4
4
Enter your number 5
5
Enter your number 6
6
Enter your number 7
7
Enter your number 8
8
Enter your number 9
9
Before method: 
0 1 2 3 4 5 6 7 8 9 
After the method: 
0 1 -1 -1 4 -1 6 -1 8 9 

【讨论】:

  • 我认为检查素数的 x = 1 和 0 的条件实际上可以放在 for 循环中。它需要休息;打破for循环
【解决方案2】:

我马上想到的主要逻辑问题是您没有正确检查素数。您应该从 2 到数组中的特定数字循环迭代,检查除数。相反,您从 2 迭代直到列表的长度。试试这个版本:

public static void prime(int[] list) {
    for (int i=0; i < list.length; ++i) {
         int num = list[i];
         boolean isPrime;
         if (num == 1) {
             isPrime = false;
         }
         else {
             isPrime = true;
         }

         for (int j=2; j <= Math.sqrt(num); ++j) {
             if (num % j == 0) {
                 isPrime = false;
                 break;
             }
         }

         if (isPrime) {
             list[i] = -1;
         }
    }

    return;
}

public static void main(String args[]) {
    int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    prime(list);
    System.out.println(Arrays.toString(list));
}

以上main()打印:

[1, -1, -1, 4, -1, 6, -1, 8, 9, 10]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多