MaelPJ
MaelPJ

Reputation: 115

Equation using variables from user input

My program is wrote in French, but just to give a brief explanation of my problem, this is something that happens every time I try to make a program that calculates a results from variables coming from a user input, using an equation.

Says the user gives his weight, the amounts of alcool he drank and the strenght of this alcool.

This equation would determines is blood alcool:

blood alcool = (amout * strenght) / (weight *0.4)

The 4 variables are before the program, declared and initialized to be able to give the variable (blood alcool) the value of the equation itself.

But the result always gives NaN, as if the program doesn't use the user input for the variable, but the initialized 0 for all of them.

I normally get around that by simply not initializing the variables, and putting the equation where is needed the results inside the program, inside a loop containing all the user input, ex:

int v1;
int v2;
int v3;

while (..) {
v1=read.input
v2=read.input
v3= v1/v2
}

I wonder if there is an another way to make the compilator use the user input for the equation, even if the variables have been initialized at the beginning of the main.

If it can make my request, here is my code, and while it compiles, for the variable alcoolemie, I get NaN.

public class Alcoolemie {
    public static void main ( String [] args) {
        // Constantes
        final String MSG_SOL_QTE_BOISSON = "Entrez la quantité de boisson"
            + " consommée(onces) :";
        final String MSG_ERR_QTE_BOISSON = "Erreur, la quantité doit être entre"
            + " 1.0 et 80.0 onces." ;
        final String MSG_SOL_TAUX_ALCOOL = "Entrez le taux d'alcool de la "
            + "boisson consommée(%): " ; 
        final String MSG_ERR_TAUX_ALCOOL = "Erreur, le taux doit être entre"
            + " 0.5 % et 100.0 %." ;
        final String MSG_SOL_POIDS = "Entrez le poids de la personne(kilos) :" ;
        final String MSG_ERR_POIDS = "Erreur, le poids doit être entre 25.0 et 200.0"
            + " kilos." ;

        // declaration de variables
        double onces= 0 ; // quantité d'alcool consommée par la personne
        double tauxAlcool= 0; // taux d'alcool de la boisson
        double poids= 0 ; // poids en kilo de la personne
        double alcoolemie = (double)((onces*tauxAlcool)/(poids*0.4)); // taux d'alcoolemie de la personne

        // sollicitation de la quantite d'alcool consommee
        System.out.println(MSG_SOL_QTE_BOISSON);
        onces = Clavier.lireDouble();
        while (onces <1.0 || onces > 80.0 ) {
            System.out.println(MSG_ERR_QTE_BOISSON);
            System.out.println(MSG_SOL_QTE_BOISSON);
            onces = Clavier.lireDouble();
        }

        // sollicitation du taux d'alcool de la boisson consommee
        System.out.println(MSG_SOL_TAUX_ALCOOL);
        tauxAlcool = Clavier.lireDouble();
        while (tauxAlcool <0.5 || tauxAlcool > 100.0 ) {
            System.out.println(MSG_ERR_TAUX_ALCOOL);
            System.out.println(MSG_SOL_TAUX_ALCOOL);
            tauxAlcool = Clavier.lireDouble();
        }

        // sollicitation du poids de la boisson consommee
        System.out.println(MSG_SOL_POIDS);
        poids = Clavier.lireDouble();
        while (poids <25.0 || poids > 200.0 ) {
            System.out.println(MSG_ERR_POIDS);
            System.out.println(MSG_SOL_POIDS);
            poids = Clavier.lireDouble();
        }

        // Affichage du taux d'alcoolemie
        System.out.println("Votre taux d'alcool dans le sang est de" + alcoolemie
        + "\n");
        if (alcoolemie >= 0.08)  { // Message taux d'alcoolemie dangeureux
            System.out.println("Attention! Votre taux d'alcool dans le sang dépasse"
                + "la limite sécuritaire de 0.08 .") ;
        }
    }
}

Thanks.

Upvotes: 0

Views: 53

Answers (2)

Deepak Jain
Deepak Jain

Reputation: 63

NaN stands for "Not a number". It comes when an arithmetic operation is undefined. for example 0.0/0.0 or square root of a negative number. In your case you are taking all values 0 which result in calculating (0.0*0.0)/(0.0/0.4)=(0.0/0.0) which is undefined because of float values. If you use 0/0 for integers it will give Arithmetic exception. Try not to use c=0.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44813

double alcoolemie = (double)((onces*tauxAlcool)/(poids*0.4)); 

has to be done after

System.out.println(MSG_SOL_POIDS);
poids = Clavier.lireDouble();
while (poids <25.0 || poids > 200.0 ) {
        System.out.println(MSG_ERR_POIDS);
        System.out.println(MSG_SOL_POIDS);
        poids = Clavier.lireDouble();
}

Upvotes: 0

Related Questions