jeudi 18 février 2016

printing an Array in java with inputs from if-statement

Please, I need help printing these arrays. In the method "IsPrime" it is testing if the inputs are prime and stores them in the array "prime[]" I set it to that big of an array because I can't possibly know the exact prime numbers the inputs will get and the same goes for the"palin"array. At the last for loop I am trying to print the results of the array of prime but the output I get are not the primes. I get "0000000000" thanks!

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

public class p1c {

private static Scanner scan;

public static void main(String[] args){

scan = new Scanner(System.in);

    int z;
    for(z = 0; z<2; z++){

    System.out.println("\n Please enter the first integer number:" );
    int numx = scan.nextInt();

    System.out.println("Please enter the second integer number");
    int numy = scan.nextInt();

    if ( numx < numy){ 
        isPrime(numx,numy);

        }

    else{
        System.out.print("This is invalid, try again, x must be less than y");
        System.exit(0);
    }
    }
}


public static int reverse(int i){

    int reverse = 0;
    while (i!= 0){
        reverse = reverse * 10;
        reverse = reverse + i % 10;
        i = i/10;

}
    return reverse;
}


public static void isPrime(int numx, int numy){
    int d = 0,c;
    int prime[] = new int[50];
    int palin[] = new int[50];

    for (int i=numx; i <= numy; i++ ){

    for (c=2; c<i; c++){
        int n = i%c;
            if (n==0){
                break;
            }
            }

                if(i == c && i != 0 && c != 0){

                    prime[i]=i;

                }

                    if( reverse(i)== i){
                        int palindrome = i;
                        palin[palindrome]= palindrome;


                }
                }   

        for(int count = 0; count < prime.length;count ++){
            d++;

            System.out.print(prime[count]);

            if(d == 10){

                System.out.println();
                d=0;
            }

        }
            }

        }

Aucun commentaire:

Enregistrer un commentaire