Hej alle sammen, jeg har et lille problem.
Jeg har brugt nogle system.out.printf() statements, bl.a. til at vise nogle decimaltal, men problemet er at den viser mange decimaler efter kommaet, og det skal den ikke.
Findes der ikke en måde hvorved man kan begrænse tallet til kun af vise 2 decimaler efter kommaet?
Hvis det har nogen hjælp, så har i min klasse her!
- /**
- * @author Carsten Jensen
- * @version 24 April 2011
- */
- public class BMI {
-
- //Maximum limits for different weight classifications!
- private static final double UNDERWEIGHT = 18.49;
- private static final double NORMALWEIGHT = 24.99;
- private static final double OVERWEIGHT = 29.99;
- private static final double FAT_LVL_1 = 34.99;
- private static final double FAT_LVL_2 = 39.99;
- //If higher than 39.99, you're extremely fat!
-
- //Main Method!
- public static void main(String[] args) {
- double weight = 91.4;
- double height = 1.85;
-
- double BMI = calculateBMI(weight, height);
- int classification = getWeightClassification(BMI);
- String label = getClassificationLabel(classification);
-
- printHelloMessage();
- printBMIMessage(weight, height, BMI, label);
- }
-
- /**
- * Calculate your BMI.
- * @param weight in Kilograms.
- * @param height in Meters.
- * @return your BMI.
- */
- private static double calculateBMI(double weight, double height) {
- return (weight / (Math.pow(height, 2)));
- }
-
- /**
- * Get the weight classification as an integer.
- * @param BMI Your Body Mass Index.
- * @return The classification of your bmi.
- */
- private static int getWeightClassification(double BMI) {
- int classification = 0;
-
- if (BMI <= UNDERWEIGHT) {
- classification = 1;
- } else if (BMI > UNDERWEIGHT && BMI <= NORMALWEIGHT) {
- classification = 2;
- } else if (BMI > NORMALWEIGHT && BMI <= OVERWEIGHT) {
- classification = 3;
- } else if (BMI > OVERWEIGHT && BMI <= FAT_LVL_1) {
- classification = 4;
- } else if (BMI > FAT_LVL_1 && BMI <= FAT_LVL_2) {
- classification = 5;
- } else if (BMI > FAT_LVL_2) {
- classification = 6;
- }
-
- return classification;
- }
-
- /**
- * get our classification label.
- * @param classification the classification as an integer value.
- * @return a String with a label for the specific classification.
- */
- private static String getClassificationLabel(int classification) {
- String str;
-
- switch(classification) {
- case 1: str = "underweight"; break;
- case 2: str = "normalweight"; break;
- case 3: str = "overweight"; break;
- case 4: str = "fat"; break;
- case 5: str = "much fat"; break;
- case 6: str = "extremely fat"; break;
- default: str = "<Unknown classification>";
- }
-
- return str;
- }
-
- private static void printHelloMessage() {
- System.out.printf("Welcome to the BMI Calculator!\n"
- + "\n"
- + "%-18s Index\n"
- + "%-18s <= %f\n"
- + "%-18s > %f and <= %f\n"
- + "%-18s > %f and <= %f\n"
- + "%-18s > %f and <= %f\n"
- + "%-18s > %f and <= %f\n"
- + "%-18s > 40\n"
- + "\n"
- , "Classification", "Underweight:", UNDERWEIGHT, "Normalweight:", UNDERWEIGHT, NORMALWEIGHT, "Overweight:", NORMALWEIGHT,
- OVERWEIGHT, "Fat:", OVERWEIGHT, FAT_LVL_1, "Much Fat:", FAT_LVL_1, FAT_LVL_2, "Extremely fat:");
- }
-
- private static void printBMIMessage(double weight, double height, double BMI, String label) {
- System.out.printf("Your weight is %f, and your height is %f.\n"
- + "This means that your BMI is %f, and you're\n"
- + "therefore to be classified as %s.\n"
- + "\n"
- + "Congratulations!"
- , weight, height, BMI, label);
- }
-
- }
Med Venlig Hilsen
Carsten Jensen
Indlæg senest redigeret d. 26.04.2011 14:14 af Bruger #16571