2012-04-03 4 views
0

私は解決策に非常に近いですが、私はこれを理解できません。何らかの理由で私のコードは、ランダムな年齢、高さ、重みなどを生成していません。さらに、静的メソッドgetBodyMassIndex()は静的コンテキスト行43 &から参照できません。エラーは、これを適用するためのインスタンスを作成する必要があるが、私は失われているので、私はすでにそれを行って何もできないと思った。これは基本的に自己教えられたJavaコースであり、 。Javaのヘルプが必要です

package classlab3b; 

import classlab3B.BodyMassIndex; 
import java.text.DecimalFormat; 
import java.util.Random; 
import java.util.Scanner; 

/** 
* 
* @author ccity 
*/ 
public class ClassLab3B { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     System.out.println("Please enter the number of people:"); 
     //asks user to input number of people 
     int numberOfPeople; 
     //declares integer variable for number of people 
     Scanner input = new Scanner(System.in); 
     //creates system input scanner 
     numberOfPeople = input.nextInt(); 
     //captures user input for number of people 
     BodyMassIndex[] a = new BodyMassIndex[numberOfPeople]; 
     //creates an array of BodyMassIndex the size of numberOfPeople 
     String name = loadRandomNames(a); 
     //loads object with random name 
     int age = loadRandomAges(a, numberOfPeople); 
     //loads object with random age 
     double weight = loadRandomWeights(a); 
     //loads object with random weight 
     double height = loadRandomHeights(a); 
     //loads object with random height 
     createObjectsToFillArray(a, name, age, weight, height, numberOfPeople); 
     //creates "x" objects to fill the array 
     double BMI = BodyMassIndex.getBodyMassIndex(); 
     //gets BMI from getBodyMassIndex method in BodyMassIndex.java 
     String status = BodyMassIndex.getStatus(); 
     //gets status from getStatus method in BodyMassIndex.java 
     //double BMI = BodyMassIndex.bmix.getBodyMassIndex(); 
     //String status = BodyMassIndex.bmix.getStatus(); 
     printArray(a, name, age, weight, height, BMI, status); 
     //prints array 


     System.out.println(" "); 
     System.out.println("Current Population"); 
     System.out.println(" "); 
     System.out.println("Obese: " + BodyMassIndex.numberOfObese); 
     System.out.println("Overweight: " + BodyMassIndex.numberOfOverweight); 
     System.out.println("Normal: " + BodyMassIndex.numberOfNormal); 
     System.out.println("Underweight: " + BodyMassIndex.numberOfUnderweight); 
     System.out.println("================"); 
     System.out.println("Total: " + BodyMassIndex.totalNumberOfPeople); 


    } 

    public static void createObjectsToFillArray(BodyMassIndex[] data, String name, int age, double weight, double height, int numberOfPeople) { 
     for (int i = 0; i < numberOfPeople; i++) { 
      data[i] = new BodyMassIndex(name, age, weight, height); 
     } 


     //creates new BodyMassIndex objects with generated variables from methods within 
    } 

    public static String loadRandomNames(BodyMassIndex[] data) { 

     String[] arrayOfFirstNames = {"Joe", "Donna", "Ronald", "Sarah", "David", "Courtney", "Irwin", "Linda", "Michael", "Cindy", "Tom", "Rebekah", "Todd", "Tracy", "Peter", "Nicole", "Marcelo", "Jennifer", "Rick", "Andrea", "Bruce", "Jaclyn", "Doug", "Shirley", "Steve", "Liz", "Waldo", "Theresa", "Scott", "Colby", "Beth", "Larry", "Emily", "Paul", "Kate", "Sam", "Dianne", "Dustin", "Alethea", "Wayne", "Kristina", "Christian", "Danny", "Breya", "Andrew", "Alison", "Tim", "Mary", "Chris", "Susie", "Jeremy", "Willy", "Jessica", "Marcus", "Kelly", "Kyle", "Stephanie", "Isaiah", "Hillary", "Eric", "Julia", "Donald", "Meredith", "Kevin", "Leslie", "Blake", "Angela", "Cliff", "Debbie", "Dylan", "Erin", "Alex", "Monica", "Nathan", "Wendy", "Josh", "Megan", "Adam", "Michelle", "Carey", "Ashley", "Brian", "Jason", "Melanie", "Jim", "Monica", "Jamie", "Rhonda", "Steven", "Perry", "Byron", "Laura", "Harry", "Brooke", "Drew", "Vicki", "Gary", "Anita", "Felipe", "Josie"}; 
     String[] arrayOfLastNames = {"Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor", "Washington", "Jefferson", "Lincoln", "Hamilton", "Jackson", "Grant", "Franklin", "McKinley", "Cleveland", "Madison", "Chase", "Nicholson", "Fauver", "Doe", "Southard", "Schmidt", "Hodson", "McDonald", "Stickley", "Miller", "Combs", "Bohus", "Krippner", "Amtower", "Banks", "Wallace", "Bannister", "Dehaven", "Yost", "Still", "Timbrook", "Peters", "Vaught", "Shellhammer", "Andrews", "Krippner", "McAlister", "Wright", "Kensinger", "McClellan", "Ganoe", "Shiley", "Layman", "Gearhart", "Yost", "Kushnir", "Bush", "Lowder", "Connolly", "Lowman", "Terveen", "Staton", "Settle", "Tinsman", "Nichols", "Baker", "Walters", "Dawe", "Renner", "Michaels", "Faircloth", "Looker", "Hastings", "Vaughan", "Anderson", "Zimmerman", "Deere", "Daher", "Lauck", "Stottlemyer", "Clinton", "Obama", "Reagan", "Montgomery", "Pugh", "Gavis", "Clark", "Bowers"}; 

     String first = get(arrayOfFirstNames); 
     String last = get(arrayOfLastNames); 
     String name = first + " " + last; 

     return name; 
    } 

    public static String get(String[] array) { 
     Random generator = new Random(); 
     int rnd = generator.nextInt(array.length); 
     return array[rnd]; 
    } 

    public static int loadRandomAges(BodyMassIndex[] data, int numberOfPeople) { 
     double min = 13; 
     double max = 99; 
     int age = 0; 
     for (int i = 0; i < numberOfPeople; i++) { 
      age = (int) randomInt(min, max); 
     } 

     return age; 
    } 

    public static double randomInt(double min, double max) { 

     double random = (double) ((max - min + 1) * Math.random() + min); 
     return random; 

    } 

    public static double loadRandomWeights(BodyMassIndex[] data) { 
     double min = 100; 
     double max = 300; 
     double weight = randomInt(min, max); 
     for (int row = 0; row < data.length; row++) { 
     } 
     return weight; 
    } 

    public static double loadRandomHeights(BodyMassIndex[] data) { 
     double min = 55; 
     double max = 80; 
     double height = randomInt(min, max); 
     for (int row = 0; row < data.length; row++) { 
     } 
     return height; 
    } 

    public static void printArray(BodyMassIndex[] data, String name, int age, double weight, double height, double BMI, String status) { 
     System.out.println(" Name   " + "Age " + "Height " + "Weight " + "BMI " + "Status"); 
     for (int i = 0; i < data.length; i++) { 

      DecimalFormat format = new DecimalFormat(); 
      format.setMinimumFractionDigits(2); 
      format.setMaximumFractionDigits(2); 


      System.out.println(name + "  " + age + "  " + format.format(height) + " " + format.format(weight) + format.format(BMI) + " " + status); 
     } 
    } 
} 

をそしてここでBodyMassIndex.javaコードです::すべてのヘルプは大歓迎!!コードは以下の

package classlab3B; 

/** 
* 
* @author Liz 
*/ 
public class BodyMassIndex { 

    private String name; 
    private int age; 
    private double weight; 
    private double height; 
    public static final double OBESE_BMI = 30.0; 
    public static final double OVERWEIGHT_BMI = 25.0; 
    public static final double NORMAL_BMI = 18.5; 
    public static int numberOfObese; 
    public static int numberOfOverweight; 
    public static int numberOfNormal; 
    public static int numberOfUnderweight; 
    public static int totalNumberOfPeople; 

    public static void main(String[] args) { 


     BodyMassIndex bmi1 = new BodyMassIndex("John Doe", 18, 145, 70); 
     //BodyMassIndex bmix = new BodyMassIndex(ClassLab3B.name, ClassLab3B.age, ClassLab3B.weight, ClassLab3B.height); 


     System.out.println("The BMI for " + bmi1.getName() + " is " + bmi1.getBodyMassIndex() + " " + bmi1.getStatus()); 

     System.out.println(" "); 
     System.out.println(" "); 
     System.out.println("Current Population"); 
     System.out.println(" "); 
     System.out.println("Obese: " + numberOfObese); 
     System.out.println("Overweight: " + numberOfOverweight); 
     System.out.println("Normal: " + numberOfNormal); 
     System.out.println("Underweight: " + numberOfUnderweight); 
     System.out.println("================"); 
     System.out.println("Total: " + totalNumberOfPeople); 
    } 

    public BodyMassIndex(String name, int age, double weight, double height) { 
     this.name = name; 
     this.age = age; 
     this.weight = weight; 
     this.height = height; 
     totalNumberOfPeople++; 
    } 

    public double getBodyMassIndex() { 
     double bmi = ((weight * 703)/(height * height)); 
     return Math.round(bmi * 100)/100.0; 
    } 

    public String getStatus() { 
     double bmi = getBodyMassIndex(); 
     if (bmi < 16) { 
      numberOfUnderweight++; 
      return "seriously underweight"; 
     } else if (bmi < 18) { 
      numberOfUnderweight++; 
      return "underweight"; 
     } else if (bmi < 24) { 
      numberOfNormal++; 
      return "normal weight"; 
     } else if (bmi < 29) { 
      numberOfOverweight++; 
      return "overweight"; 
     } else if (bmi < 35) { 
      numberOfObese++; 
      return "grossly overweight"; 
     } else { 
      numberOfObese++; 
      return "gravely overweight"; 
     } 
    } 

    @Override 
    public String toString() { 
     return "BodyMassIndex{" + "name=" + name + ", age=" + age + ", weight=" + weight + ", height=" + height + '}'; 
    } 

    public String getName() { 
     return name; 
    } 
} 
+0

あなたはハイライトする必要がありますエラーが発生した行(私は手動で数えるつもりはありません) - loadRandomAges()は、ageを選択したoncesも何もしません。 – John3136

答えて

0
  • public double getBodyMassIndex()staticとする必要があります。内部でメンバーデータを使用していないため、このメソッドを呼び出すには実際にBodyMassIndexのインスタンスは必要ありません。

したがって、このメソッドのシグネチャは、次のようになります

public static double getBodyMassIndex() {...} 
  • ある範囲の内部整数を取得するための標準的なパターンである。

    最小+(INT)(Math.random ((Max-Min)+1))

The Java Math libra ry関数Math.random()は[0,1]の範囲でdouble値を生成します。この範囲には1が含まれていないことに注意してください。

特定の範囲の値を取得するには、最初に対象とする値の範囲の大きさを掛ける必要があります。

Math.random() * (Max - Min) 

これは、[0,Max-Min)の範囲の値を返します。

Math.random() * 5 

これは範囲の値を返します[0,5)

は今、あなたはあなたがターゲットとしている範囲には、この範囲をシフトアップする必要があります:あなたは[5,10]をしたい場合は、使用して あなたは5つの整数値をカバーする必要があります。これを行うには、最小値を追加します。

Min + (Math.random() * (Max - Min)) 

これで、[Min,Max)という値が得られます。私たちの例に続いて、それが[5,10)を意味します

5 + (Math.random() * (10 - 5)) 

しかし、これはまだあるマックス、あなたは二重の値を取得している含まれていません。最大値を取得するには、範囲パラメータ(Max - Min)に1を加えて、小数部分をintにキャストして切り捨てる必要があります。これは次の方法で実現します:

Min + (int)(Math.random() * ((Max - Min) + 1)) 

あなたが持っています。範囲[Min,Max]で、または例あたりのランダムな整数値here

幸運から盗ま[5,10]

5 + (int)(Math.random() * ((10 - 5) + 1)) 

+0

sooooありがとう!私はそれを修正した。とてもシンプルなことはとてもイライラしています。私はなぜすべてのエントリのために同じ名前、年齢、体重などを得ているのか、今のアイデア? – user1309594

+0

@ user1309594私の編集 – aviad

-1

あなたBodyMassIndexのインスタンスを必要とする:あなたはそれを使用

BodyMassIndex bmi = new BodyMassIndex(...) 
double BMI = bmi.getBodyMassIndex(); 

方法は、方法は、BMIクラスの静的なものとして宣言されていることを意味します。 like:

public static double getBodyMassIndex() {...} 

しかし、それは素晴らしいデザインではありません。

+0

の静的メソッドの呼び出しに必要な 'BodyMassIndex'のインスタンスはありません。 – aviad

+0

ええ、私はgetBodyMassIndex()を呼び出す2つの方法があることを意味しました。明示的なインスタンスまたは静的な方法で試してみたが、メソッドは静的でなければならないインスタンスメソッド。 – boskop

+0

BTWもしメソッドが 'static'であれば、' BodyMassIndex bmi = null; 'と' bmi.getBodyMassIndex() 'を呼び出してもまだ動作します(これをチェックしてください:) – aviad

0

これらの2つの方法が静的ではありませんので、はいあなたがClassLab3Bと呼ばれるクラスからBodyMassIndex.getBodyMassIndex();BodyMassIndex.getStatus();にアクセスすることはできません。..

uはそれらを静的にする場合は、uはウルクラスBodyMassIndexの非静的メンバにアクセスできるように文句を言いません..

だからuがcreateObjectsToFillArrayからBodyMassIndex []()、その後は、forループ内の各インスタンスにアクセス戻し、この

よう printArray()

何かを呼び出すことができます

BodyMassIndex[] x= createObjectsToFillArray(a, name, age, weight, height, numberOfPeople); 
    //creates "x" objects to fill the array 

    for(int i=0;i<x.length;i++) 
     { 
     double BMI = x[i].getBodyMassIndex(); 
     //gets BMI from getBodyMassIndex method in BodyMassIndex.java 
     String status = x[i].getStatus(); 
     //gets status from getStatus method in BodyMassIndex.java 
     //double BMI = BodyMassIndex.bmix.getBodyMassIndex(); 
     //String status = BodyMassIndex.bmix.getStatus(); 
     printArray(a, name, age, weight, height, BMI, status); 
     } 

public static BodyMassIndex[] createObjectsToFillArray(BodyMassIndex[] data, String name, int age, double weight, double height, int numberOfPeople) { 
    for (int i = 0; i < numberOfPeople; i++) { 
     data[i] = new BodyMassIndex(name, age, weight, height); 
    } 
return data; 

    //creates new BodyMassIndex objects with generated variables from methods within 
} 
1

ダブルBMI = BodyMassIndex。 getBodyMassIndex(); 文字列ステータス= BodyMassIndex。 getStatus();

この2つの方法で何をしていますか この2つのメソッドを静的ではなくクラス名を使用して呼び出して作成しています。これら2つのメソッドを静的変数として作成する場合、この2つのメソッドでは静的変数も使用しません。

double [] BMI = new double [numberOfPeople];
String [] status = new String [numberOfPeople];

がため(INT i = 0; I < = numberOfPeople-1; I ++)は{

BMI [i]は= [I] .getBodyMassIndex()。

status [i] = a [i] .getStatus();

}

公共の静的な無効プリントアレイ(BodyMassIndex []データ、文字列名、int型の年齢、ダブル体重、ダブル高さ、重[] BMI、String []型の状態)