0

私は現在bmi計算機を作成しようとしていますが、2つの異なる値(フィートとインチ)をキャッチして確認する方法がわからないため、人の身長のフィートとインチをインチ単位の1つの値にしようとしています。彼ら。

パブリッククラス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 に答える