输入身高时,我已经为男人和女人的理想体重编码了。

import java.util.Scanner;
public class height
{
public static void main (String []args)
{
    int Feet, Inches, Totalinches, Maleweight, Femaleweight;
    Scanner scan = new Scanner (System.in);

    System.out.println ("Please enter your height in feet and inches...");

    System.out.println ("Feet: ");
    Feet = scan.nextInt();
    System.out.println ("Inches: ");
    Inches = scan.nextInt();

    Totalinches = Feet*12 + Inches;
    Maleweight = 106 + (Totalinches - 60)*6;
    Femaleweight = 100 + (Totalinches - 60)*5;

    System.out.println ("The ideal weight for a " + Feet + " foot " + Inches +  " male is " + Maleweight + " pounds.");

    System.out.println ("A weight in the range  to  is okay.");

    System.out.println ("The ideal weight for a " + Feet + " foot " + Inches + " female is " + Femaleweight + " pounds.");

    System.out.println ("A weight in the range  and  is okay.");
}


}

上面写着“范围内的重量...”,我需要输入代码,其中包含计算理想重量范围的公式。可以找到一张图表,其中列出了与身高相对应的所有理想体重范围:

BMISurgery

我感谢您提供的每一个小帮助,非常感谢

最佳答案

如果要使用该表中的值而无需重复公式来计算它们,则可以使用一些映射来存储它们

    Map<Integer, Integer> minimumMaleWeight = new HashMap<>();
    minimumMaleWeight.put(54, 63);
    minimumMaleWeight.put(55, 68);
    minimumMaleWeight.put(56, 74);
    minimumMaleWeight.put(57, 79);

    Map<Integer, Integer> maximumMaleWeight = new HashMap<>();
    maximumMaleWeight.put(54, 77);
    maximumMaleWeight.put(55, 84);
    maximumMaleWeight.put(56, 90);
    maximumMaleWeight.put(57, 97);

    System.out.println(minimumMaleWeight.get(Totalinches));
    System.out.println(maximumMaleWeight.get(Totalinches));


如果您想使用公式来计算它们,则看起来舍入为5.4 * Totalinches-228.7可能有效(here is where I got the numbers,您可以对最大重量执行相同的操作)

10-02 22:48