java - How to utilize a switch statement in this instance? -
i'm writing program converts units (ex. oz & lb) new units of measurement (g & kg). going have user input unit want convert , value of original unit. set constants convert selected units. i'm having issue using switch statement determine conversion should use (oz ---> g, oz --->, etc).
here's 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); }
}
concatinate 2 strings , use case statement. if user inputs oz initial measurement, , g final measurement, have switch(initial + final) yield: ozg.
have case statement check each:
string orig = input.next(); string end = input.next(); switch(orig + end){ case "ozg": //do mathemetical function here result of oz --> g } }
Comments
Post a Comment