0

我正在尝试添加 Sedentary、Somewhat_Active、Active 和 Very_Active 来计算 BMR。代码将编译但不会计算。这是第一次尝试,所以任何建议都是有帮助的。我已经成功创建了一个没有添加类别的 BMR,但现在我无法进行计算。在我还添加 MaleBMR 之前,我试图让女性 BMR 先工作

import java.util.Scanner;

public class BMR2

{ 
  public static void main(String[] args)
  {

    Scanner kybd = new Scanner(System.in);

    System.out.print("Enter Height: ");

    double Height = kybd.nextDouble ();

    System.out.print("Enter Weight: ");

    Double Weight = kybd.nextDouble ();

    System.out.print("Enter Age: ");

    int Age = kybd.nextInt ();

    double FemaleBMR = 655 + 4.3 * Weight + 4.7 * Height - 6.8 * Age;

    double MaleBMR = 66 + (6.3 * Weight) + (12.9 * Height) - (6.8 * Age);

    int choc = 230;
    System.out.println("Are you Sedentary, Somewhat_Active, Active, or Very_Active?: ");
    boolean Sedentary = kybd.nextBoolean ();
    boolean Somewhat_Active = kybd.nextBoolean();
    boolean Active = kybd.nextBoolean ();
    boolean Very_Active = kybd.nextBoolean ();

    if (Sedentary)
      System.out.println("chocolate bars for female: "+ FemaleBMR*1.2/choc);

    else if (Somewhat_Active)
    System.out.println("chocolate bars for female: "+ FemaleBMR*1.3/choc);

    else if (Active)
      System.out.println("chocolate bars for female: "+FemaleBMR*1.4/choc);

    else if (Very_Active)
      System.out.println("chocolate bars for female: "+FemaleBMR*1.5/choc);

    }
    }

编译但不会计算产量

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextBoolean(Scanner.java:1756)
at BMR2.main(BMR2.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
4

1 回答 1

0

我严重怀疑这张海报是在为塞拉学院的 DoBeck 教授的 CS12 或 CS13 课程做实验室作业。我现在在他的课上做同样的事情。

无论如何,

您应该从在顶部声明您的字段开始。使其更具可读性和更易于调试/维护。

改变双倍权重 = kybd.nextDouble(); 将权重加倍 = kybd.nextDouble();

对于生活方式选择评估器(久坐、活跃、非常活跃),使用 switch 语句。

在此处了解 switch 语句:http: //docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

下面是我的版本。注意注释中的空格和适当的文档,以使代码更具可读性、可理解性和可维护性。

// 代码从下面开始

导入 java.util.Scanner;

公共类 BMRCalculator

{

公共静态无效主要(字符串参数[])

{

// 字段

double BMRMale, BMRFemale; // male and female bmr calculation fields

double height; // inches
double weight; // pounds (lbs)

final int maleOption = 1;

final int femaleOption = 2;

int userChoice;

int age = 0;


Scanner kb = new Scanner ( System.in ); // creates a keyboard scanner object called: kb   


  System.out.print("What is your age: ");

  age = kb.nextInt();


  System.out.print("Enter your height (inches): ");

  height = kb.nextDouble();

  System.out.print("Enter your weight (pounds): ");

  weight = kb.nextDouble();




  // male or female gender selction prompt

  System.out.print ("Enter 1 for male or 2 for female gender selection: ");

  userChoice = kb.nextInt();

/*

*卡路里燃烧计算:

*哈里斯本尼迪克特方法

* BMR 男子:= 66 +(6.23 X 体重,磅)+(12.7 X 身高,英寸)-(6.8 X 年龄)

* BMR 女性:= 655 +(4.35 X 体重,磅)+(4.7 X 身高,英寸)-(4.7 X 年龄)

*/

if (userChoice >= 1 && userChoice <= 2)

{

  switch( userChoice )

  {
     case maleOption:

 //  BMR Men: = 66 + (6.23 X weight in pounds) + (12.7 X height in inches) – (6.8 X age)

     BMRMale = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * age);

     System.out.println ("Your Male BMR is: " + BMRMale );


     break; // break out of maleOption


     case femaleOption:

// BMR Women: = 655 + (4.35 X weight in pounds) + (4.7 X height in inches) – (4.7 X age)


     BMRFemale = 655 + ( 4.35 * weight ) + (4.7 * height ) - (4.7 * age );

     System.out.println ("Your Female BMR is: " + BMRFemale );

     break; // break out of femaleOption




  } // end switch case

} // 如果块结束

else // 如果未输入 1 或 2,则会显示错误。

{

     System.out.println("Invalid gender choice. Please enter 1 for male, 2 for female !");



} // end if .. else block

} // 结束主程序

} // 结束类

于 2014-04-06T02:51:31.780 回答