0

我目前正在尝试创建一个 bmi 计算器,但由于我不知道如何捕捉两个不同的值(英尺和英寸)然后验证他们。

公共类 BMI {

public static void main (String[] args)
{
    System.out.println("Value between 2 and 7: " + heightInInches(2,7));
    System.out.println("Value between 0 and 11: " + heightInInches(0,11));

    System.out.println("Value between 3 and 30: " + weightInPounds(3,30));
    System.out.println("Value between 0 and 13: " + weightInPounds(0,13));
}

public static int heightInInches(int lower, int upper)
{
    Scanner kybd = new Scanner(System.in);
    System.out.printf("Enter your height between %d and %d: ", lower, upper);
    int height = kybd.nextInt();

    while (height < lower || height > upper)
    {
        System.out.printf("Retry between %d and %d:" , lower, upper);
        height = kybd.nextInt();
    }     

    return height;

}
4

4 回答 4

1

这看起来不对while (height < inches || height > inches)

事实上,整个heightInInches()必须被修改。您通过显示英尺和英寸来接受用户输入的方式是不正确的。

于 2012-10-29T14:43:05.403 回答
0

如果你想保留你的代码,我不推荐。像这样将扫描仪保持在while循环中

while(yourcond){
   kybd = new Scanner(System.in);//take the input again
     height = kybd.nextInt();
}
于 2012-10-29T14:45:24.520 回答
0

可能您想查询用户的英尺,将其存储在成员变量中,然后查询英寸并进行计算。例子:

修改后的主类:

    package feetinch;

    public class FeetInch {

    public static void main(String[] args) {
        HeightQuery heightQuery = new HeightQuery();

        heightQuery.queryForFeet();
        heightQuery.queryForInches();

        System.out.println("Result=" + heightQuery.getHeight());


    }
}

新班级:

    package feetinch;

import java.util.Scanner;

public class HeightQuery {
    private int feet, inches;

    public void queryForFeet() {
        Scanner kybd = new Scanner(System.in);
        System.out.printf("Enter your height in feet: ");
        feet = kybd.nextInt();
    }

    public void queryForInches() {
        Scanner kybd = new Scanner(System.in);
        System.out.printf("Enter your height in inches: ");
        inches = kybd.nextInt();
    }

    public int getHeight() {
        return (feet * 12 )  + inches;
    }

}
于 2012-10-29T14:58:41.260 回答
0

以下是一个简单的BMI计算器,您可以通过修改自己的条件来玩。

static int heightInInches( int f,int i){
            int in=f*12;
            int inches=i+in;
            return inches;
        }
        static int weightInPounds(int stone, int p){
            int po=stone*14;
            int pounds=p+po;
            return pounds;
        }
        static int calculateBMI(int height,int weight ){
            int w=weight*703;
            int h=height*height;
            int bmi = w/h;
            return bmi;
        }
        public static void main(String[] args){
            Scanner input=new Scanner(System.in);
            System.out.print("Enter height in feet (2-7): ");
            int feet=input.nextInt();
            while( !(feet>= 2 && feet<=7))
            {
                System.out.println("Retry between 2 and 7 :");
                feet=input.nextInt();
            }
            System.out.print("Enter height in inch(0-11): ");
            int inch=input.nextInt();
            while( !(inch>= 0 && inch<=11))
            {
                System.out.println("Retry between 0 and 11 :");
                inch=input.nextInt();
            }
            System.out.print("Enter weight in stone: ");
            int stone=input.nextInt();
            System.out.print("Enter weight in pound: ");
            int pound=input.nextInt();
            int height=heightInInches(feet,inch);
            int weight=weightInPounds(stone,pound);
            int bmi=calculateBMI(height,weight);
            System.out.println("BMI is: "+bmi);
        }
于 2012-10-29T15:29:44.003 回答