2016-11-22 3 views
-3

Я не могу понять, что мне нужно сделать, поскольку мне необходимо создать калькулятор с классами. Любая помощь будет оценена по достоинству.Калькулятор BMI с использованием классов

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(); Я не совсем уверен, что я должен положить в эти скобки, так как я ничего не могу вложить туда, не пропуская меня.

+0

Вы используете переменные, которых не существует. –

+1

Описание проблемы очень неясное. Одна действительно очевидная проблема: outputBMI принимает два параметра 'int heightInInches, int weightInPounds', но вы вызываете его ни с чем:' outputBMI(); ' – tnw

+0

Я не совсем уверен, как я называю его параметрами, как все, что я набрал isn ' t работает? приветствие за помощь! – Luke

ответ

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

0

В объектно-ориентированном образом. Было бы разумно создать три класса 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()); 
    } 
} 
Смежные вопросы