我是Java新手......我有一个正在讨论方法的任务。出于某种原因,当我调用我的方法并将数据传回我的主要内容时,所有内容都是“-Infinity”或“0”。我一直试图解决这个问题两天,我似乎无法找到合适的解决方案。
我只包括作业第一部分的代码,因为我觉得无论我犯了什么错误......我在整个作业中都是这样做的。因此,如果有人可以帮助我完成这一部分,那么我也希望能够纠正我的其他问题。
第一种方法返回:-Infinity,但是当我将代码分开并在不使用方法的情况下运行它时...我得到11.8,这是正确的。
非常感谢任何帮助!
/*
* Anthony Vincenzo Laginess
* CIT 130
* Oct. 12th, 2016
* HMW 07 - Methods
* Time Needed:
*/
package cit130hmw07_laginess;
import java.util.Scanner;
public class CIT130HMW07_Laginess {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//*************************************************
//****************** Method 1 *********************
//*************************************************
//This method calculates body fat. It takes your gender as a parameter
//and outputs your bodyfat.
System.out.println("Please enter your gender...");
String gender = input.nextLine();
System.out.println("Please enter your weight: ");
int bodyWeight = input.nextInt();
System.out.println("Please enter your waist measurement: ");
int waistSize = input.nextInt();
if(gender.equalsIgnoreCase("male")) {
bodyFatMale(bodyWeight, waistSize);
double bodyFatPercentage = bodyFatMale(bodyWeight, waistSize);
System.out.println("Your body fat is: " + bodyFatPercentage); }
else if(gender.equalsIgnoreCase("female")) {
System.out.println("Please enter your waist size: ");
int waist = input.nextInt();
System.out.println("Please enter your hip size: ");
int hips = input.nextInt();
System.out.println("Please enter your forearm size: ");
int forearms = input.nextInt();
bodyFatFemale(bodyWeight, waistSize, waist, hips, forearms);
double answer = bodyFatFemale(bodyWeight, waistSize, waist, hips,
forearms);
System.out.println("Your body fat is: " + answer); }
else
//enter an error message
}//main
//METHOD 1: Bodyfat calculations
public static double bodyFatMale(int bodyWeight, int waistSize) {
int weight = 0;
int waist = 0;
double A1;
double A2;
double B;
double actualBodyFat;
double bodyFatPercentage;
A1 = (weight * 1.082) + 94.42;
A2 = waist * 4.15;
B = A1 - A2;
actualBodyFat = weight - B;
bodyFatPercentage = actualBodyFat * 100 / weight;
return bodyFatPercentage; }
public static double bodyFatFemale(int bodyWeight, double wristSize, double waistSize, double hipSize, double forearmSize) {
int weight = 0;
double wrist = 0;
double waist = 0;
double hips = 0;
double forearms = 0;
double A1, A2, A3, A4, A5, B;
double actualBodyFat;
double bodyFatPercentage;
A1 = (weight * .732) + 8.987;
A2 = wrist / 3.14;
A3 = waist * 0.157;
A4 = hips * 0.249;
A5 = forearms * 0.434;
B = A1 + A2 - A3 - A4 + A5;
actualBodyFat = weight - B;
bodyFatPercentage = actualBodyFat * 100 / weight;
return bodyFatPercentage; }
}//class
答案 0 :(得分:1)
在每个方法中,您将所有变量设置为0,因此方法内的计算将使用0执行。相反,您需要将变量分配给您作为参数传入的值。
所以而不是
int weight = 0;
试
int weight = bodyWeight;
答案 1 :(得分:0)
提示:在bodyFat
方法中,您有两个体重和腰围尺寸的变量。你使用的是错误的;即你已经初始化为ero的那个。你应该只有一个。
答案 2 :(得分:0)
您实际上并未在计算中使用方法参数。
这是第一种方法的固定版本:
//METHOD 1: Bodyfat calculations
public static double bodyFatMale(int bodyWeight, int waistSize) {
double A1;
double A2;
double B;
double actualBodyFat;
double bodyFatPercentage;
A1 = (bodyWeight* 1.082) + 94.42;
A2 = waistSize* 4.15;
B = A1 - A2;
actualBodyFat = bodyWeight- B;
bodyFatPercentage = actualBodyFat * 100 / bodyWeight;
return bodyFatPercentage;
}
答案 3 :(得分:0)
public static double bodyFatMale(int bodyWeight, int waistSize) {
int weight = 0; // Introducing `0` into all calculations
.....
A1 = (weight * 1.082) + 94.42; // Should be `bodyWeight`?
A2 = waist * 4.15; // Should be `bodyWeight`?
B = A1 - A2;
actualBodyFat = weight - B; // Should be `bodyWeight`?
bodyFatPercentage = actualBodyFat * 100 / weight; // Should be `bodyWeight`?