【问题标题】:BMI calculator using classes使用类的 BMI 计算器
【发布时间】:2026-01-19 19:30:01
【问题描述】:

我不知道该放什么,因为我需要创建带有类的计算器。任何帮助将不胜感激。

    import java.util.*;
public class BMI
{
    public static void main(String[] args)
    {
        heightInInches();
        weightInPounds();
        outputBMI();

    }
    public static void heightInInches()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("What is your height in feet between 2 and 7? " );
        int feet = input.nextInt();

        while (feet < 2 || feet > 7)
        {
          System.out.print("Retry between 2 and 7: ");
          feet = input.nextInt();
        }

        System.out.println("How many inches between 0 and 11? " );
        int inches = input.nextInt();

        while (inches < 0 || inches > 11)
        {
          System.out.print("Retry between 0 and 11: ");
          inches = input.nextInt();

        }
        int actualHeight = (feet * 12) + inches;

        System.out.println("You are this tall in inches: " + actualHeight); 
    }
    public static int weightInPounds()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("What is your weight in stone between 3 and 30? " );
        int stone = input.nextInt();

        while (stone < 3 || stone > 30)
        {
          System.out.print("Retry between 3 and 30: ");
          stone = input.nextInt();
        }

        System.out.println("How many pounds between 0 and 13? " );
        int pounds = input.nextInt();

        while (pounds < 0 || pounds > 13)
        {
          System.out.print("Retry between 0 and 13: ");
          pounds = input.nextInt();
        }
        int actualWeight =(stone * 14) + pounds;
        System.out.println("You are this heavy in pounds: " + actualWeight); 
        return actualWeight;

    }
    public static void outputBMI(int heightInInches, int weightInPounds)
    {
        double BMI = (weightInPounds * 703) / (heightInInches * heightInInches);

        System.out.println("This is your BMI: " + BMI);

    }
}

这里是 outputBMI();我不太确定我应该在这些括号中放什么,因为我不能放任何东西而不会给我一个错误。

【问题讨论】:

  • 您正在使用不存在的变量。
  • 您的问题描述非常不清楚。一个非常明显的问题:outputBMI 有两个参数int heightInInches, int weightInPounds,但你没有调用它:outputBMI();
  • 我不确定如何使用参数调用它,因为我输入的所有内容都不起作用?为帮助喝彩!
  • main 里面你这样称呼它outputBMI(8, 200); 你可以使用这个键盘开始codepad.remoteinterview.io/PISVGARCRS

标签: java


【解决方案1】:

您没有将任何参数传递给 outputBMI 方法。

还可以考虑使用Double's 而不是Integers's,这样你就可以少问问题:)

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        int height = heightInInches();
        int weight = weightInPounds();
        outputBMI(height, weight);

    }

    public static int heightInInches()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("What is your height in feet between 2 and 7? " );

        int feet = input.nextInt();

        while (feet < 2 || feet > 7)
        {
          System.out.print("Retry between 2 and 7: ");
          feet = input.nextInt();
        }

        System.out.println("How many inches between 0 and 11? " );
        int inches = input.nextInt();

        while (inches < 0 || inches > 11)
        {
          System.out.print("Retry between 0 and 11: ");
          inches = input.nextInt();

        }

        int actualHeight = (feet * 12) + inches;
        System.out.println("You are this tall in inches: " + actualHeight); 
        return actualHeight;
    }

    public static int weightInPounds()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("What is your weight in stone between 3 and 30? " );
        int stone = input.nextInt();

        while (stone < 3 || stone > 30)
        {
          System.out.print("Retry between 3 and 30: ");
          stone = input.nextInt();
        }

        System.out.println("How many pounds between 0 and 13? " );
        int pounds = input.nextInt();

        while (pounds < 0 || pounds > 13)
        {
          System.out.print("Retry between 0 and 13: ");
          pounds = input.nextInt();
        }
        int actualWeight =(stone * 14) + pounds;
        System.out.println("You are this heavy in pounds: " + actualWeight); 
        return actualWeight;

    }

    public static void outputBMI(int heightInInches, int weightInPounds)
    {
        double BMI = (weightInPounds * 703) / (heightInInches * heightInInches);

        System.out.println("This is your BMI: " + BMI);
    }
}

在这里试试:https://repl.it/E5fv/0

【讨论】:

    【解决方案2】:

    以面向对象的方式。创建三个类别 Weight、Height 和 BMI 是有意义的。见下文。这种方法封装了从英寸和磅到 BMI 公式的各种转换。

    import java.util.*;
    
    class Weight {
      static private int STONE_TO_POUNDS = 14;
      private Integer weightInPounds;
    
      void setWeight(int stone, int pounds) {
        weightInPounds = (stone * STONE_TO_POUNDS) + pounds;
      }
    
      Integer getWeightInPounds() {
        return weightInPounds;
      }
    }
    
    class Height {
       static private int FEET_TO_INCHES = 12;
       private Integer heightInInches;
    
      void setHeight(int feet, int inches) {
          heightInInches = (feet * FEET_TO_INCHES) + inches;
       }
    
      Integer getHeightAsInches() {
        return heightInInches;
    
      }
    }
    
    class BMI {
      private Height height;
      private Weight weight;
    
      void setHeight(Height height) {
         this.height = height;
      }
    
      void setWeight(Weight weight) {
        this.weight = weight;
      }
    
      double calculate() {
          return (weight.getWeightInPounds() * 703) / (height.getHeightAsInches().intValue() * height.getHeightAsInches().intValue());
      }
    }
    
    
    public class Main
    {
        public static void main(String[] args)
        {
            BMI bmi = new BMI();
    
    
            bmi.setHeight(getHeight());
            bmi.setWeight(getWeight());
    
            outputBMI(bmi);
    
        }
    
        public static Height getHeight()
        {
            Height height = new Height();
            Scanner input = new Scanner(System.in);
            System.out.println("What is your height in feet between 2 and 7? " );
    
            int feet = input.nextInt();
    
            while (feet < 2 || feet > 7)
            {
              System.out.print("Retry between 2 and 7: ");
              feet = input.nextInt();
            }
    
            System.out.println("How many inches between 0 and 11? " );
            int inches = input.nextInt();
    
            while (inches < 0 || inches > 11)
            {
              System.out.print("Retry between 0 and 11: ");
              inches = input.nextInt();
    
            }
    
            height.setHeight(feet, inches);
            System.out.println("You are this tall in inches: " + height.getHeightAsInches()); 
    
            return height;
        }
    
        public static Weight getWeight()
        {
            Weight weight = new Weight();
            Scanner input = new Scanner(System.in);
    
            System.out.println("What is your weight in stone between 3 and 30? " );
            int stone = input.nextInt();
    
            while (stone < 3 || stone > 30)
            {
              System.out.print("Retry between 3 and 30: ");
              stone = input.nextInt();
            }
    
            System.out.println("How many pounds between 0 and 13? " );
            int pounds = input.nextInt();
    
            while (pounds < 0 || pounds > 13)
            {
              System.out.print("Retry between 0 and 13: ");
              pounds = input.nextInt();
            }
    
    
            weight.setWeight(stone, pounds);
            System.out.println("You are this heavy in pounds: " + weight.getWeightInPounds()); 
            return weight;
    
        }
    
        public static void outputBMI(BMI bmi)
        {
    
            System.out.println("This is your BMI: " + bmi.calculate());
        }
    }
    

    【讨论】: