简体   繁体   中英

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. 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. 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. 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. 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) . If you have a calculation, same deal. Write if (x < 5) , not 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).

Fix this style error (of writing == true ) - not just here but in general in how you program. 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. Either start the for loop by setting it to false, or set it false after you added the current element to the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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