This question already has an answer here:
- How do I compare strings in Java? 23 answers
Why does the code doesn't work if I input "H" (without quotes) or "M" (without quotes)?
import java.util.*; public class Pesoideal {
public static void main(String[] args) {
// TODO Auto-generated method stub
String genero = "";
int altura = 0;
do{
genero = JOptionPane.showInputDialog("Introduce tu género: H o M");
}while(genero.equalsIgnoreCase("H")==false && genero.equalsIgnoreCase("M")==false);
if(genero=="H"){
altura = Integer.parseInt((JOptionPane.showInputDialog("Introduce tu altura:")));
int pesoideal = altura - 120;
System.out.println("Su peso ideal sería " + pesoideal + " kg ");
}
else if(genero=="M"){
altura = Integer.parseInt((JOptionPane.showInputDialog("Introduce tu altura")));
int pesoideal = altura - 110;
System.out.println("Su peso ideal sería " + pesoideal + " kg ");
}
}
Aucun commentaire:
Enregistrer un commentaire