2016-10-15 3 views
0

私はJavaにまったく新しい...メソッドについて議論している課題があります。何らかの理由で私のメソッドを呼び出し、データを私のメインに戻すときには、 "-Infinity"または "0"です。私はこれを2日間直ちに解決しようとしており、適切な解決策を見つけることができないようです。Javaメソッドを呼び出してデータを返す

私は間違いが何であっても気づいているので、私は割り当ての最初の部分のコードだけを含んでいます...私は割り当て全体を通してそれを作ります。だから誰かがこの部分で私を助けることができれば、私はうまくいけば私の他の問題も訂正することができます。

最初の方法は次のようになります。 - 無限大ですが、コードを分割してメソッドを使用せずに実行すると、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 
+1

誰もそれに対処しているように思わないので、-Infinityは、二重のゼロ除算によるものです。 – ChiefTwoPencils

答えて

0

ヒント:bodyFatの方法では、あなたは体重とウエストサイズのための2つの変数を持っています。あなたは間違ったものを使っています。つまり、あなたがエロに初期化したものです。あなたは1つしか持っていないはずです。

1

各メソッドでは、すべての変数を0に設定しているため、メソッド内の計算は0で実行されます。代わりに、パラメータとして渡す値に変数を割り当てる必要があります。

ので、代わりの

int weight = 0; 

は、あなたが実際にあなたの計算で、あなたのメソッドのパラメータを使用していない

int weight = bodyWeight; 
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; 
} 
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`?     
関連する問題