Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Why there is no output in my code?

solrac8686

New Coder
Java:
package ejercicio2;

import java.util.ArrayList;
import java.util.Random;

public class Ejercicio2 {
    private static ArrayList<Double> array = new ArrayList<Double>();

    public static ArrayList<Double> rellenar(double min, double max, int tamaño) {
        Random random = new Random();
        for (int i = 0; i < tamaño; i++) {
            double valor = random.nextDouble(max - min + 1) + min;
            array.add(valor);
        }
            return array;
    }

    public static String escribir(ArrayList<Double> array) {
        String cadena = "[";
        for (int i = 0; i < array.size(); i++) {
            double valor = array.get(i);
            if (i == array.size() - 1) {
                cadena = cadena + valor + "]";
            } else {
                cadena = cadena + valor + ", ";
            }
        }

        return cadena;
    }

    public static double media(ArrayList<Double> array) {

        double media = 0;
        double suma = 0;
        for (int i = 0; i < array.size(); i++) {
            suma = suma + array.get(i);
        }
        media = suma / array.size();
        return media;
    }

    public static int menorMedia(ArrayList<Double> array) {

        int menores = 0;
        double media = media(array);
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i) < media) {
                menores++;
            }
        }
        return menores;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        array = rellenar(-20, 50, 20);
        System.out.println(escribir(array));
        System.out.println(media(array));
        System.out.println(menorMedia(array));
    }

}

I'm learning java and ive been a lot of time trying to solve this but i'm not able to do it pls help!
 
Seems to be outputting for me...do you have your console hidden?


Rich (BB code):
[46.71589034084988, -15.532088843285749, 14.592210546014854, -2.21839594856079, -3.8681578272422357, 48.4170294425693, 3.4691326677430965, 11.50642829946661, 48.3776175002651, 30.106924816215127, -12.44230425624647, 5.06434485465563, 38.398060590279165, 41.05853363445637, 39.397895907655695, -18.207940456362124, -0.9429396023456178, 10.967568165476152, 37.483262328763345, -17.74343632032006]
15.229981792002363
12
 
Well, there was some issue woth your code, here is the correct code and I hope you will get what you are looking for.

Code:
package ejercicio2;


import java.util.ArrayList;
import java.util.Random;


public class Ejercicio2 {
    private static ArrayList<Double> array = new ArrayList<Double>();


    public static ArrayList<Double> rellenar(double min, double max, int tamaño) {
        Random random = new Random();
        for (int i = 0; i < tamaño; i++) {
            double valor = random.nextDouble() * (max - min) + min;
            array.add(valor);
        }
        return array;
    }


    public static String escribir(ArrayList<Double> array) {
        String cadena = "[";
        for (int i = 0; i < array.size(); i++) {
            double valor = array.get(i);
            if (i == array.size() - 1) {
                cadena = cadena + valor + "]";
            } else {
                cadena = cadena + valor + ", ";
            }
        }


        return cadena;
    }


    public static double media(ArrayList<Double> array) {
        double media = 0;
        double suma = 0;
        for (int i = 0; i < array.size(); i++) {
            suma = suma + array.get(i);
        }
        media = suma / array.size();
        return media;
    }


    public static int menorMedia(ArrayList<Double> array) {
        int menores = 0;
        double media = media(array);
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i) < media) {
                menores++;
            }
        }
        return menores;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        array = rellenar(-20, 50, 20);
        System.out.println(escribir(array));
        System.out.println(media(array));
        System.out.println(menorMedia(array));
    }
}


Thanks
 

Buy us a coffee!

Back
Top Bottom