我正在尝试通过添加久坐,Somewhat_Active,Active和Very_Active来计算BMR。代码将编译但不会计算。这是第一次尝试,所以任何建议都是有帮助的。我已经成功创建了没有添加类别的BMR,但现在我无法进行计算。在尝试添加MaleBMR之前,我试图让FemaleBMR首先工作
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)
答案 0 :(得分:0)
我很怀疑这张海报是在塞拉学院为DoBeck教授的CS12或CS13课程做实验课。我现在正在他班上做同样的事情。
反正,
您应该首先在顶部声明您的字段。使其更易读,更容易调试/维护。
更改Double Weight = kybd.nextDouble();加倍= kybd.nextDouble();
对于生活方式选择评估者(久坐,活跃,非常活跃),请使用switch语句。
在此处了解switch语句:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
以下是我的版本。注意注释中的空格和适当的文档,以使代码更易读,易懂和易于维护。
//代码开始
import java.util.Scanner;
公共类BMRCalculator
{
public static void main(String args [])
{
//字段
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
} //结束if block
else //如果未输入1或2,则显示错误。
{
System.out.println("Invalid gender choice. Please enter 1 for male, 2 for female !");
} // end if .. else block
} //结束主要
} //结束课