0

Im learning programming with my group of friends and we are seriously stucked with something that looks pretty easy at first sight.

Basically we have a for loop that runs once. Inside we get some user input. Then we use a conditional to establish if we should add +1 to countM (Male count) or +1 to countF (Female count) instead.

PROBLEM -> The issue comes on the conditionals. When the user input is "F", the programm should be executing countF++, (if the requirements are fullfiled) however countM++ is the one executing everysingle time even if the user input is F and not M.

public static void main(String[] args) {

    int countM = 0;
    int countF = 0;

    int peso;
    int estatura;
    String sexo;


    for (int i = 1; i <= 2; i++) {


        if(i == 2){ 
          JOptionPane.showMessageDialog(null, "El nº de chicos aptos es " + countM);
          JOptionPane.showMessageDialog(null, "El nº de chicas aptas es " + countF);
          break;
        } 

        JOptionPane.showMessageDialog(null, "Teclea los datos del alumno nº "+ i);


        estatura = Integer.parseInt(JOptionPane.showInputDialog("Teclea la estatura del alumno (cm)"));
        peso = Integer.parseInt(JOptionPane.showInputDialog("Teclea el peso del alumno en kg"));
        sexo = JOptionPane.showInputDialog("Teclea el sexo del alumno (M o F)");



        if(sexo == "F"){
            if(estatura>160 && peso>60){
            countF++;
            }
        }

        else{
            if(estatura>170 && peso>70){
            countM++;
            }
        }               
    }       
}
Adrian Danlos
  • 447
  • 6
  • 20

1 Answers1

2

use sexo.equalsIgnoreCase("f"); instead of sexo=="f"

And add a check that user only inputs M or F else it would still do CountM++

Stultuske
  • 9,296
  • 1
  • 25
  • 37