简体   繁体   English

用户在给定整数数组中查找素数的方法

[英]Method to find prime numbers in a given array of integers by user

i am trying to write a method in JAVA that receives an array of integers from the user , and returns an ArrayList of prime numbers that were in that array.我正在尝试在 JAVA 中编写一个方法,该方法从用户接收整数数组,并返回该数组中素数的 ArrayList。 i tried to first verify if each element of array is a prime number or not.我试图首先验证数组的每个元素是否是素数。 and then i added the ones that were prime, in an ArrayList and in the end return the ArrayList.然后我在 ArrayList 中添加了素数,最后返回了 ArrayList。 But, when i run the code it returns all the numbers, even though the logic looks correct to me.但是,当我运行代码时,它会返回所有数字,即使逻辑在我看来是正确的。

Here is my code :这是我的代码:

package numeriPrimi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class NumeriPrimi {

    public static ArrayList arrayPrimi(int[] number) {
        ArrayList<Integer> listaPrimi = new ArrayList<>(); 
        boolean ris = false ;
        for(int i=0 ; i<number.length ; i++) {
            if (number[i] == 2) {
                ris = true;
            } 
            else { 
                if (number[i] != 2 && number[i] % 2 == 0 ) {
                    ris = false ; 
                } 
                else {
                    if(number[i]<=3) {
                        for(int j=3 ; j < number[i]; j++) {
                            if(number[i] % j == 0 ) {
                               ris = false ; }
                            }
                        }
                    }
                }   
                if(ris=true) {
                    listaPrimi.add(number[i]);
                }
            }
            return listaPrimi; 
        }

    public static void main(String[] args) {
        Scanner input= new Scanner(System.in);
        System.out.println("how many elements does the array have?");
        int l= Integer.parseInt(input.nextLine());
        int[] numbers = new int[l]; 
        
        System.out.println("enter the array of numbers?");
        
        for(int i=0 ; i< numbers.length ; i++) {
            numbers[i] = input.nextInt();
        }
        System.out.println("the array is : " + Arrays.toString(numbers));
        ArrayList<int[]> lista = NumeriPrimi.arrayPrimi(numbers);
        System.out.println("the Prime numbers in the array "+ Arrays.toString(numbers) + " are as follows : \n" + NumeriPrimi.arrayPrimi(numbers));
    }

}

ris = true makes the value of ris equal to true and the entire expression then resolves to that value. ris = true品牌的值ris等于true ,然后整个表达式解析到该值。 In other words:换句话说:

if (ris = true) System.out.println("hello");

is identical to:等同于:

ris = true;
System.out.println("hello");

You should never write == true in java, for any reason.无论出于何种原因,您都不应该在 Java 中编写== true It's always pointless.这总是毫无意义的。 Only true is true, so let's say you have a variable x which is a boolean , don't write if (x == true) , just write if (x) .只有true是真的,所以假设你有一个boolean变量x ,不要写if (x == true) ,只写if (x) If you have a calculation, same deal.如果你有一个计算,同样的交易。 Write if (x < 5) , not if ((x < 5) == true) .if (x < 5) ,而不是if ((x < 5) == true)

Doing that = foo trick pretty much at any other time (ie with non-booleans) wouldn't suffer from this problem: if (x = 5) doesn't compile ( if (x == 5) does).在任何其他时间(即使用非布尔值)执行那个= foo技巧几乎不会遇到这个问题: if (x = 5)不编译( if (x == 5) )。

Fix this style error (of writing == true ) - not just here but in general in how you program.修复此样式错误(写作== true )-不仅在这里,而且通常在您的编程方式中。 In passing this bug will go away.顺便说一下,这个错误会消失。

It's a bit hard to follow, but you never set the "ris" value back to false, so you will end up adding everything after the first ris = true.这有点难以理解,但您永远不会将“ris”值设置回 false,因此您最终会在第一个 ris = true 之后添加所有内容。 Either start the for loop by setting it to false, or set it false after you added the current element to the list.通过将其设置为 false 来启动 for 循环,或者在将当前元素添加到列表后将其设置为 false。

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

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