vendredi 23 janvier 2015

If-else statements made my program stop immediately -- Java

I'm just going to post what I've got so far in a program that is supposed to do the following:

- Calculate BAC (blood alcohol content) based on inputs given by the user and determine, with the various inputs, whether or not they would get a DUI (be over the limit based on inputs given).

So here's my code:

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 + ".");
}
}


...

It may not be the best way to do it, but any help is appreciated. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire