if-else语句使我的程序立即停止 - Java

时间:2015-01-24 05:33:43

标签: java if-statement

我将在一个应该执行以下操作的程序中发布到目前为止我所得到的内容:
- 根据用户给出的输入计算BAC(血液酒精含量),并根据各种输入确定是否会获得DUI(超过基于给定输入的限制)。
所以这是我的代码:
import java.util。*;

public class Proj2_Mazzone
{
    public static void main (String[] args)
    {

        Scanner scan = new Scanner(System.in);

        String gender;
        int D, W, H, age;
        double ratio, limit, alcAbsorbed, alcMetabolized, BAC;

...

//BAC is calculated here, based on given input
//Make 4 diff scenarios using && with if statements...
//So f21, fnot21, m21, mnot21
System.out.print("Enter M if you're a male, or F if you're a female: ");
    gender = scan.nextLine();
    //males over 21
    if(gender.equalsIgnoreCase("m") && age > 21){
        ratio = .73;
        limit = .08;
        alcAbsorbed = (3.701 * D) / (W * ratio);
        alcMetabolized = .015 * H;
        BAC = alcAbsorbed - alcMetabolized;
        if(BAC >= limit){
            System.out.println("You're over the limit!");
            System.out.println("Your BAC is " + BAC + "!");
            }
            else if(BAC <= limit){
                System.out.println("You're good...for now.");
                System.out.println("Your BAC is " + BAC + ".");
                }
        }
    //males under 21
    if(gender.equalsIgnoreCase("m") && age < 21){
        ratio = .73;
        limit = .02;
        alcAbsorbed = (3.701 * D) / (W * ratio);
        alcMetabolized = .015 * H;
        BAC = alcAbsorbed - alcMetabolized;
        if(BAC >= limit){
            System.out.println("You're over the limit!");
            System.out.println("Your BAC is " + BAC + "!");
            }
            else if(BAC <= limit){
                System.out.println("You're good...for now.");
                System.out.println("Your BAC is " + BAC + ".");
                }
        }

    //females over 21
    if(gender.equalsIgnoreCase("f") && age > 21){
        ratio = .66;
        limit = .08;
        alcAbsorbed = (3.701 * D) / (W * ratio);
        alcMetabolized = .015 * H;
        BAC = alcAbsorbed - alcMetabolized;
        if(BAC >= limit){
            System.out.println("You're over the limit!");
            System.out.println("Your BAC is " + BAC + "!");
            }
            else if(BAC <= limit){
                System.out.println("You're good...for now.");
                System.out.println("Your BAC is " + BAC + ".");
                }
        }

    //females under 21
    if(gender.equalsIgnoreCase("f") && age < 21){
        ratio = .66;
        limit = .02;
        alcAbsorbed = (3.701 * D) / (W * ratio);
        alcMetabolized = .015 * H;
        BAC = alcAbsorbed - alcMetabolized;
        if(BAC >= limit){
            System.out.println("You're over the limit!");
            System.out.println("Your BAC is " + BAC + "!");
            }
            else if(BAC <= limit){
                System.out.println("You're good...for now.");
                System.out.println("Your BAC is " + BAC + ".");
                }
        }

...
它可能不是最好的方式,但任何帮助都表示赞赏。提前谢谢。

5 个答案:

答案 0 :(得分:0)

else if语句中的真正问题是其中一个可能不会触发。不要在if else if中执行if(BAC<=limit)语句,而是执行以下操作:

    if (BAC >= limit) {
        System.out.println("You're over the limit!");
        System.out.println("Your BAC is " + BAC + "!");
    } else {
        System.out.println("You're good...for now.");
        System.out.println("Your BAC is " + BAC + ".");
    }

另外,你有没有读过你的年龄?

答案 1 :(得分:0)

不是一次又一次地编写相同的代码,而是应该创建一个通用函数。这将需要2个参数,即比率和限制。 除此之外,所有其他计算对于所有年龄限制和男性或女性都是常见的。

在该功能中进行计算并简单打印 它会缩短您的代码并使其更具可读性和可理解性。

答案 2 :(得分:0)

存在一个逻辑问题:21岁的女性堕落。您的主要信息是:

if(gender.equalsIgnoreCase("f") && age > 21){
    ...
}

if(gender.equalsIgnoreCase("f") && age < 21){
    ....
}

如果年龄正好是21岁,则不会输入任何一个块。

答案 3 :(得分:0)

我建议您在发现问题后重构此代码以减少重复。还要尽量避免if语句可能导致无法运行代码,而是使用if-else语句 - 你总是要打印某些东西,否则感觉就像程序停止一样。

也许是这样的

public static void main (String[] args)
{
    // Scan variables etc

    // calculate ratio
    double ratio = -1;
    if (gender.equalsIgnoreCase("m"))
    {
        ratio = .73;        
    }
    if (gender.equalsIgnoreCase("f"))
    {
        ratio = .66;
    }

    // calculate limit
    double limit = -1
    if (age > 21)
        limit = .08;
    limit = .02;

    // calculate blood alcohol level
    alcAbsorbed = (3.701 * D) / (W * ratio);
    alcMetabolized = .015 * H;
    double bloodAlcoholLevel = alcAbsorbed - alcMetabolized;

    if(bloodAlcoholLevel >= limit)
    {
        System.out.println("You're over the limit!");
        System.out.println("Your BAC is " + BAC + "!");
    }
    else
    {
        System.out.println("You're good...for now.");
        System.out.println("Your BAC is " + BAC + ".");
    }
}

这样您的代码可以更短更容易阅读,更不用说改变了。 : - )

答案 4 :(得分:0)

你的问题来自21岁女性的条件错误!她被排除在所有案件之外。

此外,您的代码难以调试,因为它难以阅读 考虑以下重构,我在其中创建了确定值的方法 我为这样一个简单的代码走得很远,但它告诉你应该从不复制/粘贴代码。当你重复代码时,这意味着你可以将它放在一个方法中,该方法将使用正确的参数多次调用。

public class StackOverflow {
    // TODO define the real values!
    private static final int D = 0, W = 0, H = 0;


    public static void main(String[] args) {
        // Get the input
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter M if you're a male, or F if you're a female: ");
        String gender = scan.nextLine();
        System.out.print("Enter your age: ");
        int age = Integer.valueOf(scan.nextLine());

        // Get the good values
        double ratio = getRatio(gender);
        double bac = getBAC(ratio);
        double limit = getLimit(age);

        // Print the result
        if (bac <= limit) {
            System.out.println("You're good...for now.");
        } else {
            System.out.println("You're over the limit!");
        }
        System.out.println("Your BAC is " + bac + "!");
    }

    /**
     * This method returns the ratio according to the gender.
     */
    private static double getRatio(String gender) {
        if ("m".equalsIgnoreCase(gender)) {
            return .73;
        } else if ("f".equalsIgnoreCase(gender)) {
            return .66;
        }
        throw new IllegalArgumentException("Unknown gender: " + gender);
    }

    /**
     * This method returns the BAC according to the ratio.
     */
    private static double getBAC(double ratio) {
        double alcAbsorbed = (3.701 * D) / (W * ratio);
        double alcMetabolized = .015 * H;
        return alcAbsorbed - alcMetabolized;
    }

    /**
     * This method returns the limit according to the age.
     */
    private static double getLimit(int age) {
        if (age < 21) {
            return .02;
        } else {
            return .08;
        }
    }
}