2

当我输入母亲身高和父亲身高时,他们将计算孩子的身高。但是我如何添加一个循环,所以当孩子的高度为零或更低时,应该给出一个错误消息并重试?

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);
    }
}
4

2 回答 2

4

如下使用 do -while 循环来实现此目的。

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);
    }

}

于 2013-05-23T14:45:21.973 回答
0

使用递归,类似下面的东西应该可以工作

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);
    }
}
于 2013-05-23T13:36:22.250 回答