How can I solve this program? Java Cycle For

0

It is necessary to develop a program that allows managing the information to organize the fifth edition of a marathon. It is known that:

  • For organizational matters, a maximum amount of 15,000 is allowed (but I put 5 in the program to be able to verify that I am well asked) participants.
  • The distances of the race are (in km):

  • 10
  • 5
  • 2
  • The sizes of the shirts can be:

  • 'S'
  • 'M'
  • 'L'
  • It will be offered:

  • 1 bottle of water for each 10K runner
  • 0.5 bottle of water for each runner of 5K
  • 1 bottle of water every 4 runners of the 2K
  • You want:

  • To be able to enter the 15,000 participants
  • Take the following data:

  • Age
  • Distance running
  • Dress shirt
  • Calculate:

  • Average age of participants
  • Number of runners per distance
  • Number of shirts per size that must be purchased (This point would not be coming out and I do not know how to do it.)
  • Quantity of water bottles that must be purchased
  • My code so far is this:

        int participantes = 5;
        int km = 0;
        int km10 = 0;
        int km5 = 0;
        int km2 = 0;
        String talles = " ";
        int talleS = 0;
        int talleM = 0;
        int talleL = 0;
        int botella = 0;
        int edad;
        int edadPromedio;
        int sumatoriaEdad = 0;
    
        Scanner teclado = new Scanner(System.in);
    
        for (int i = 1; i < participantes; i++) {
    
            System.out.println(i + ".Ingrese la edad : ");
            edad = teclado.nextInt();
            edad++;
            System.out.println(i + ".Ingrese la distancia que recorre : ");
            km = teclado.nextInt();
            System.out.println(i + ".Ingrese talle : ");
            talles = teclado.next();
    
            switch (km) {
    
            case 10:
    
                if (km == 10) {
    
                    System.out.println("Se ofrece 1 botella de agua.");
    
                    km10++;
                    botella++;
    
                }
    
                break;
    
            case 5:
    
                if (km == 5) {
    
                    System.out.println("Se ofrece 0.5 botella de agua.");
    
                    km5++;
                    botella++;
    
                }
    
                break;
    
            case 2:
    
                if (km == 2) {
    
                    if ((i % 4) == 0) {
    
                        System.out.println("Se ofrece 1 botella de agua cada cautro corredores.");
    
                        botella++;
                    }
    
                    km2++;
    
                }
    
                break;
    
            default:
    
                System.out.println("Error! Distancia inexistente");
    
                break;
    
            }
    
            switch (talles) {
    
            case "S":
    
                if (talles == "S") {
    
                    if (i == 1) {
    
                        talleS++;
    
                    }
    
                }
    
                break;
    
            case "M":
    
                if (talles == "M") {
    
                    if (i == 1) {
    
                        talleM++;
    
                    }
    
                }
    
                break;
    
            case "L":
    
                if (talles == "L") {
    
                    if (i == 1) {
    
                        talleL++;
    
                    }
    
                }
    
                break;
    
            default:
                System.out.println("Error! talle inesxistente.");
    
                break;
    
            }
    
            sumatoriaEdad += edad;
    
        }
    
        edadPromedio = sumatoriaEdad / 5;
    
        System.out.println("Edad promedio : " + edadPromedio);
    
        System.out.println("Cantidad de corredores por distancia : ");
        System.out.println("Corredores de km 10 : " + km10);
        System.out.println("Corredores de km 5 : " + km5);
        System.out.println("Corredores de km 2 : " + km2);
    
        System.out.println("Cantidad de remeras por talle que se deben comprar  : ");
        System.out.println("Talle S : " + talleS);
        System.out.println("Talle M : " + talleM);
        System.out.println("Talle L : " + talleL);
    
        System.out.println("Cantidad de botellas de agua que se deben comprar : " + botella);
    
        
    asked by computer96 04.10.2018 в 18:21
    source

    1 answer

    3

    The problem is here:

    if (i == 1) {
    
        talleS++;
    
    }
    

    I'll explain, you're going from 1 to the number of participants, which by the way, should be up to + 1 participants for the for loop, otherwise, by removing the participant "0" as the total number of participants will always be less one. I'm not sure what you wanted to put or why you put that if, but seeing your code, it's only going to be executed once! Therefore, waistlines, waistlines and waistlines will always be worth 1, even if you put a million participants.

    When you enter the data of participant number 2, you will not add any of the sizes since i == 2 and not 1 as you put in the if.

    Suggestion:

    Since talles is a string and what you do is enter M, L or S, then, in position "0" of the string there will be a letter and if that letter matches M, L or S, then you have to increase the variable waist, waist, or waist, respectively. Also, I agree with what gbianchi said, if you put: case 10, you are already doing if (km == 10), it is unnecessary and impractical to put it again.

    I hope that's the problem! This would be the code that I would put:

    int participantes = 5;
    int km = 0;
    int km10 = 0;
    int km5 = 0;
    int km2 = 0;
    String talles = " ";
    int talleS = 0;
    int talleM = 0;
    int talleL = 0;
    int botella = 0;
    int edad;
    int edadPromedio;
    int sumatoriaEdad = 0;
    Scanner teclado = new Scanner(System.in);
    for (int i = 1; i < (participantes+1); i++) {
        System.out.println(i + ".Ingrese la edad : ");
        edad = teclado.nextInt();
        edad++;
        System.out.println(i + ".Ingrese la distancia que recorre : ");
        km = teclado.nextInt();
        System.out.println(i + ".Ingrese talle : ");
        talles = teclado.next();
        switch (km) {
            case 10:
                System.out.println("Se ofrece 1 botella de agua.");
                km10++;
                botella++;
                break;
            case 5:
                System.out.println("Se ofrece 0.5 botella de agua.");
                km5++;
                botella++;
                break;
            case 2:
                if ((i % 4) == 0) {
                    System.out.println("Se ofrece 1 botella de agua cada cautro corredores.");
                    botella++;
                }
                km2++;
                break;
            default:
                System.out.println("Error! Distancia inexistente");
                break;
        }
        switch (talles[0]) {
            case 'S':
                talleS++;
                break;
            case 'M':
                talleM++;
                break;
            case 'L':
                talleL++;
                break;
            default:
                System.out.println("Error! talle inexistente.");
                break;
        }
        sumatoriaEdad += edad;
    }
    edadPromedio = sumatoriaEdad / 5;
    System.out.println("Edad promedio : " + edadPromedio);
    System.out.println("Cantidad de corredores por distancia : ");
    System.out.println("Corredores de km 10 : " + km10);
    System.out.println("Corredores de km 5 : " + km5);
    System.out.println("Corredores de km 2 : " + km2);
    System.out.println("Cantidad de remeras por talle que se deben comprar  : ");
    System.out.println("Talle S : " + talleS);
    System.out.println("Talle M : " + talleM);
    System.out.println("Talle L : " + talleL);
    System.out.println("Cantidad de botellas de agua que se deben comprar : " + botella);
    
        
    answered by 04.10.2018 / 22:10
    source