Java 使用方法,我无法读取两个不同的输入

Java 使用方法,我无法读取两个不同的输入,java,methods,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

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

}
publicstaticvoidmain(字符串[]args)
{
System.out.println(“介于2和7之间的值:“+高度英寸(2,7));
System.out.println(“0到11之间的值:“+高度英寸(0,11));
System.out.println(“介于3和30之间的值:“+weightInPounds(3,30));
System.out.println(“0到13之间的值:+weightInPounds(0,13));
}
公共静态内部高度(内部较低,内部较高)
{
扫描仪kybd=新扫描仪(System.in);
System.out.printf(“输入您在%d和%d之间的高度:”,下,上);
int height=kybd.nextInt();
同时(高度<下部| |高度>上部)
{
System.out.printf(“在%d和%d之间重试:”,下限,上限);
高度=kybd.nextInt();
}     
返回高度;
}

当(高度inches)


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

如果您想保留您的代码,我不建议这样做。请将扫描仪保持在while循环内,如下所示

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

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

修改的主类:

    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;
    }

}

下面是一个简单的体重指数计算器,你可以通过修改自己的身体状况来进行计算

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);
        }
静态整数高度(整数f,整数i){
int in=f*12;
int英寸=i+英寸;
返回英寸;
}
静态整数权重整数(整数石头,整数p){
int po=石头*14;
整数磅=p+po;
归还英镑;
}
静态整数计算BMI(整数高度、整数重量){
int w=重量*703;
int h=高度*高度;
int bmi=w/h;
返回体重指数;
}
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
系统输出打印(“以英尺(2-7)为单位输入高度:”;
int feet=input.nextInt();

而!(英尺>=2&&feet=0&&inch我知道位是错误的,因为我需要验证高度,但这样做我不能有单独的英尺或英寸???你说不能有单独的英尺或英寸是什么意思?首先,你没有明确的英尺或英寸的下限或上限…请用户输入一个介于什么之间的值?我刚刚纠正了这一点,因为我已经做了如果你想让用户输入他的身高在5英尺到5英尺10英寸之间,你需要正确地指定它,或者至少在你的代码中进行基本的转换。是的,但它们都必须在一个输入中,这样才能验证值。否则,如果我有两个循环,它只会重复,即使得到它被证实了