2

When I input motherHeight and fatherHeight then they will calculate the height of the child. But how do I add a loop so when the height of the child is zero or less, that should give an error message and try again?

import java.util.Scanner;

public class estimateHeight {
    private static Scanner kb = new Scanner(System.in);
    private static Scanner kbNum = new Scanner(System.in);

    public static void calculateHeight(char gender, float motherHeight, float fatherHeight ) {
        float height = 0;
        if(gender == 'm' || gender == 'M')
            height = ((motherHeight * 13/12) + fatherHeight )/2;
        else
            height = ((fatherHeight * 12/13) + motherHeight)/2;

        if(height <= 0) {
            System.out.println("Invalid inputs, please reneter: " );
            calculateHeight(gender, motherHeight, fatherHeight);
        }
        else
            System.out.println("The child's height is: " + height);
    }

    public static void main(String[] args) {
        System.out.println("Estimating the Adult Height of a Child. Press Q or -1 to quit.\n");

        String genderSelect;
        float fatherHeight, motherHeight;

        System.out.print("Gender? (m/f): ");
        genderSelect = kb.nextLine();
        if(genderSelect.equalsIgnoreCase("q"))
            System.exit(0);
        while(!(genderSelect.equalsIgnoreCase("m") || genderSelect.equalsIgnoreCase("f"))) {
            System.out.print("Invalid. Gender? (m/f): ");
            genderSelect = kb.nextLine();
        }

        System.out.print("Father's height? ");
        fatherHeight = kbNum.nextFloat();
        if(fatherHeight == -1)
            System.exit(0);

        System.out.print("Mother's height? ");
        motherHeight = kbNum.nextFloat();
        if(motherHeight == -1)
            System.exit(0);

        calculateHeight(genderSelect.charAt(0), motherHeight, fatherHeight);
    }
}
6
  • 3
    What language are you using? That ought to be one of the tags you apply to your question. I could guess at java, but I shouldn't have to. Commented May 23, 2013 at 13:29
  • Didnt understood your gender condition gender == 'm' || gender == 'M'. What does it mean? Commented May 23, 2013 at 13:32
  • emm, you dont compare strings like that. should be foo.equals() Commented May 23, 2013 at 13:48
  • @MaciejCygan But you do compare char's like that. And those are char's. '' indicates a char (also ...(char gender,...), "" would be a string. Commented May 23, 2013 at 13:59
  • Sorry, yes it's Java. Commented May 23, 2013 at 14:28

2 Answers 2

4

Use a do -while loop as follows to achieve this.

import java.util.Scanner;

public class estimateHeight {
private static Scanner kb = new Scanner(System.in);
private static Scanner kbNum = new Scanner(System.in);

public static boolean calculateHeight(char gender, float motherHeight, float fatherHeight ) {
    float height = 0;
    if(gender == 'm' || gender == 'M')
        height = ((motherHeight * 13/12) + fatherHeight )/2;
    else
        height = ((fatherHeight * 12/13) + motherHeight)/2;

    if(height <= 0) {
        System.out.println ("Invalid details. Please Re-enter. ");
       return true;
    }
    else{            
        System.out.println("The child's height is: " + height);
        return false;
    }
}

public static void main(String[] args) {
boolean check;
    do{
        System.out.println("Estimating the Adult Height of a Child. Press Q or -1 to quit.\n");
        String genderSelect;
        float fatherHeight, motherHeight;

        System.out.print("Gender? (m/f): ");
        genderSelect = kb.nextLine();
        if(genderSelect.equalsIgnoreCase("q"))
            System.exit(0);
        while(!(genderSelect.equalsIgnoreCase("m") || genderSelect.equalsIgnoreCase("f"))) {
            System.out.print("Invalid. Gender? (m/f): ");
            genderSelect = kb.nextLine();
        }

        System.out.print("Father's height? ");
        fatherHeight = kbNum.nextFloat();
        if(fatherHeight == -1)
            System.exit(0);

        System.out.print("Mother's height? ");
        motherHeight = kbNum.nextFloat();
        if(motherHeight == -1)
            System.exit(0);

        check = calculateHeight(genderSelect.charAt(0), motherHeight, fatherHeight);
    }while(check);
    }

}

Sign up to request clarification or add additional context in comments.

Comments

0

Use recursion, something like below should work

public static void calculateHeight(char gender, float motherHeight, float fatherHeight) {
    float height = 0;
    if(gender == 'm' || gender == 'M') {
        height = ((motherHeight * 13/12) + fatherHeight)/2;
    }
    else {
        height = ((fatherHeight * 12/13) + motherHeight)/2;
    }
    if(height <= 0)
    {// recurse here
       System.out.println("Invalid inputs, please reneter: " );
        //read the inputs here, and call recursively this method     
        calculateHeight(gender, motherHeight, fatherHeight);
    }
    else
    {
        System.out.println("The child's height is: " + height);
    }
}

3 Comments

Recursion has it's uses, but here that could use up a lot of extra memory to do something as easily done in a loop.
It doesn't work. I'll post the full code if you need to check it.
+1 @Denise. Recursion should never be used for something like this. Prefer a loop like in Leo's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.