0

好的,所以我一直在尝试用java编写一个程序来根据父母的身高(英尺/英寸)计算孩子的身高,并根据用户输入的性别来估计答案。不过,我已经为此困扰了好几个小时。似乎我没有尝试做正确的计算。我是java新手,第2周..我觉得这应该很容易?也许我从中得到的比它需要的更多?

添加了我的程序完成后的外观图片。

((输出需要看起来如何的示例。))

public static void main(String[] args) 
{
Scanner scan = new Scanner(System.in);

String userChoice;
int heightMom, heightDad, FemaleChildh, MaleChildh, genderChild;

System.out.println("Enter the gender of your future child. Use 1 for female, 
0 for male.");
genderChild = scan.nextInt();

System.out.println("Enter the height in feet, then the height in inches of 
the mom.");
heightMom = scan.nextInt();

System.out.println("Enter the height in feet, then the height in inches of 
the dad.");
heightDad = scan.nextInt();

MaleChildh = (heightMom*13/12 + heightDad)/2;
FemaleChildh = (heightDad+12/13 + heightMom)/2;

if (genderChild.equals(1))
System.out.println(("Your future child is estimated to grow 
to")+FemaleChildh);

if (genderChild.equals(0))
System.out.println(("Your future child is estimaed to grow to")+MaleChildh);

System.out.println("Enter 'Y' to run again, anything else to exit.");
userChoice = scan.next();
if (userChoice.equals("Y"))
System.out.println("Continuing...");
else if (userChoice.equals(""))
break;
}
4

1 回答 1

0

将 heightmom 和 heightdad 分成英尺和英寸。使用双打来存储您的 MaleChildh 和 FemaleChildh。用“估计”一词修正错字。

编辑:看看您是否理解此代码,然后将英寸部分添加到孩子的身高。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    String userChoice;
    int heightMomFt, heightMomIn, heightDadFt, heightDadIn, genderChild;
    double FemaleChildhFt, MaleChildhFt, FemaleChildhIn, MaleChildhIn;
    while (true) {
        System.out.println("Enter the gender of your future child. Use 1 for female, 0 for male.");
        genderChild = scan.nextInt();

        System.out.println("Enter the height in feet, then the height in inches of the mom.");
        heightMomFt = scan.nextInt();
        heightMomIn = scan.nextInt();

        System.out.println("Enter the height in feet, then the height in inches of the dad.");
        heightDadFt = scan.nextInt();
        heightDadIn = scan.nextInt();

        MaleChildhFt = (heightMomFt * 13 / 12 + heightDadFt) / 2;
        FemaleChildhFt = (heightDadFt + 12 / 13 + heightMomFt) / 2;

        if (genderChild == 1)
            System.out.println(("Your future child is estimated to grow to") + FemaleChildhFt);

        if (genderChild == 0)
            System.out.println(("Your future child is estimated to grow to") + MaleChildhFt);

        System.out.println("Enter 'Y' to run again, anything else to exit.");
        userChoice = scan.next();
        if (userChoice.equals("Y"))
            System.out.println("Continuing...");
        else
            break;
    }
}
于 2018-04-08T01:09:28.487 回答