Zapełnienie pustej tablicy jednowymiarowej losowymi wartościami, w pętli do while.

0

Witam.
Tytułem wstępu chciałbym zapytać o to, czy mogę tak często pisać tematy. Codziennie robię zadania z programowania, i coraz więcej mi wychodzi. Ale z niektórymi zadaniami mam problem. Między innymi dzięki waszej pomocy, mogę się rozwijać w nauce. Jeśli tworzę tematy za często proszę o reprymendę.
Do rzeczy. Otóż mam takie zadanie : Do tablicy jednowymiarowej każdy kolejny element losuj z przedziału <4,10> dopóki suma aktualnie losowanej wartości i wszystkich poprzednich elementów tablicy nie będzie liczbą pierwszą. Wypisz tablicę na ekranie

 public class Zad1PPZestaw2 {
    public static void main(String[] args) {
        Random rnd = new Random();
        int tab[] = new int[]{};

        for (int i = 0; i < tab.length; i++) {
            do {
                tab[i] = rnd.nextInt(7) + 4;
            } while (whetherPrimeNumber(tab) != true);
        }
        System.out.println("Elements of array: " + Arrays.toString(tab));
    }

    public static int suma(int[] tab) {
        int sum = 0;
        for (int i = 0; i < tab.length; i++) {
            sum += tab[i];
        }
        return sum;
    }

    public static boolean whetherPrimeNumber(int[] tab) {
        if (suma(tab) > 2) {
            double sq = Math.sqrt(suma(tab));
            if (suma(tab) % 2 == 0)
                return false;
            else {
                for (int i = 3; i <= sq; i += 2) {
                    if (suma(tab) % i == 0) {
                        return false;
                    }
                }
                return true;
            }
        } else if (suma(tab) == 2) return true;
        return false;
    }
}

Trudność sprawia mi napisanie warunku do while, Gdyż nie znam rozmiaru tablicy i chce żeby w pętli losowane były kolejne elementy, dopóki nie zostanie spełniony warunek. W jaki sposób mogę to prawidłowo zapisać?

0

Problem jest taki, że tworzysz tablicę o rozmiarze 0 i próbujesz do niej dodawać. To się nie uda. Zamiast tablicy użyj List.

0

Przerobione, działa dzięki.

 public class Zad1PPZestaw2 {
    public static void main(String[] args) {
        Random rnd = new Random();
        ArrayList<Integer> tab = new ArrayList<Integer>();

        do {
            tab.add(rnd.nextInt(7) + 4);
        } while (whetherPrimeNumber(tab) != true);

        System.out.println("Element of the array: " + Arrays.toString(tab.toArray()));
    }


    public static int suma(ArrayList<Integer> tab) {
        int sum = 0;
        for (int i = 0; i < tab.size(); i++) {
            sum += tab.get(i);
        }
        return sum;
    }

    public static boolean whetherPrimeNumber(ArrayList<Integer> tab) {
        if (suma(tab) > 2) {
            double sq = Math.sqrt(suma(tab));
            if (suma(tab) % 2 == 0)
                return false;
            else {
                for (int i = 3; i <= sq; i += 2) {
                    if (suma(tab) % i == 0) {
                        return false;
                    }
                }
                return true;
            }
        } else if (suma(tab) == 2) return true;
        return false;
    }
}

0

Pomysł• by sumę liczb w tablicy liczyć wielokrotnie jest bardzo osobliwy.

        do {
            tab.add(rnd.nextInt(7) + 4);
            long sum = suma(tab);
        } while (!whetherPrimeNumber(sum));

A jeszcze wydajniej będzie sumować na bieżąco

 public class Zad1PPZestaw2 {
    static long sum = 0;
    public static void main(String[] args) {
        Random rnd = new Random();
        ArrayList<Integer> tab = new ArrayList<Integer>();

        do {
            int number = rnd.nextInt(7) + 4;
            tab.add(number);
            sum+=number;
        } while (whetherPrimeNumber());

        System.out.println("Elements of the array: " + Arrays.toString(tab.toArray()));
    }


    public static boolean whetherPrimeNumber() {
        if (sum > 2) {
            double sq = Math.sqrt(sum);
            if (sum % 2 == 0)
                return false;
            else {
                for (int i = 3; i <= sq; i += 2) {
                    if (sum % i == 0) {
                        return false;
                    }
                }
                return true;
            }
        } else if (sum == 2) return true;
        return false;
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1