好的,所以我试图找出whay,无论我输入什么性别,它告诉我性别无效。我的目标是让程序识别m或M或F或f作为性别,我认为我完成了.toUpperCase ...任何人都知道我缺少什么?
public class ChildHeight {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); //iniate keyboard as scanner object
String cGender;
int hMotherFeet;
int hMotherInches;
int hFatherFeet;
int hFatherInches;
int hChild;
int hMother;
int hFather;
//Prompts user for imput
System.out.print("Please enter gender for a child (ex. m,F): ");
cGender = keyboard.next();
System.out.print("Please enter the number of feet in father's height: ");
hFatherFeet = keyboard.nextInt();
System.out.print("Please enter the number of inches in father's height: ");
hFatherInches = keyboard.nextInt();
System.out.print("Please enter the number of feet in mother's height: ");
hMotherFeet = keyboard.nextInt();
System.out.print("Please enter the number of inches in mother's height: ");
hMotherInches = keyboard.nextInt();
//adds inches to the converted inches from feet
hMother = hMotherInches + ConvertToInches(hMotherFeet);
hFather = hFatherInches + ConvertToInches(hFatherFeet);
//takes parameters and based on gender points to formula to get childs height
hChild = GetChildHeight(hMother, hFather, cGender);
//Prints childs calculated height
System.out.printf("The estimated child height is %d'%d\".", (hChild / 12), (hChild % 12));
}
//method to convert feet to inches
public static int ConvertToInches(int feet)
{
return feet * 12;
}
public static int GetChildHeight(int mother, int father, String gender)
{
int child = 0;
if (gender.toUpperCase() == "M")
{
child = ((mother * 13/12) + father)/2;
}
else if (gender.toUpperCase()== "F")
{
child = ((father * 13/12) + mother)/2;
}
else
{
System.out.println("Invalid gender. Gender must be M or F.");
System.exit(0);
}
return child;
}
}