mardi 2 février 2016

How to utilize a switch statement in this instance?

I'm writing a program that converts units (ex. oz & lb) to new units of measurement (g & kg). I was going to have the user to input the unit they want to convert from and the value of the original unit. I also set constants to convert the selected units. What I'm having an issue with is using a switch statement to determine what conversion I should use (oz ---> g, oz --->, etc).

Here's the code:

    System.out.print("Convert from: ");
    String origMeasure = input.nextLine();

    System.out.print("Convert to: ");
    String newMeasure = input.nextLine();

    System.out.print("Value: ");
    double value = input.nextDouble();

    double newValue = 0;

    final double OZ_TO_G = 28.3;
    final double OZ_TO_KG = 0.028;
    final double LB_TO_G = 453.6;
    final double LB_TO_KG = 0.045;
    final double IN_TO_MM = 25.4;
    final double IN_TO_CM = 2.54;
    final double IN_TO_M = 0.0254;
    final double FT_TO_MM = 304.8;
    final double FT_TO_CM = 30.48;
    final double FT_TO_M = 0.3048;

    final String OZ = " oz";
    final String LB = " lb";
    final String IN = " in";
    final String FT = " ft";
    final String G = " g";
    final String KG = " kg";
    final String MM = " mm";
    final String CM = " cm";
    final String M = " m";

    switch(origMeasure){
    case(OZ):
        newValue = (value * OZ_TO_G);
        break;
    }
    System.out.println(value + origMeasure + "=" + newValue + newMeasure);
}

}

Aucun commentaire:

Enregistrer un commentaire