使用方法,我不能让它读取两个不同的输入

时间:2012-10-29 14:36:01

标签: java methods

我目前正在尝试创建一个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 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

while(yourcond){
   kybd = new Scanner(System.in);//take the input again
     height = kybd.nextInt();
}

答案 2 :(得分: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;
    }

}

答案 3 :(得分: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);
        }