2012-04-03 4 views
2

いいえ、私は静的エラーを修正しました。今、私はすべてのオブジェクト(つまり、同じ名前、年齢、体重など)に対して同じエントリを取得している理由を調べようとしています。ここでは、コードは次のようになります。ランダムなオブジェクトを生成する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); 
     } 
    } 
} 

私はランダムなエントリを取得する必要がありますが、私は一つだけを取得しています。ここで出力されます。

run: 
Please enter the number of people: 
4 
    Name   Age Height Weight BMI Status 
Courtney Anderson  81  79.64 155.0717.19 underweight 
Courtney Anderson  81  79.64 155.0717.19 underweight 
Courtney Anderson  81  79.64 155.0717.19 underweight 
Courtney Anderson  81  79.64 155.0717.19 underweight 

Current Population 

Obese: 0 
Overweight: 0 
Normal: 0 
Underweight: 1 
================ 
Total: 4 
+0

コードのデバッグを試しましたか? –

+1

あなたは本当に多くのコメントを必要としません^^。 – Giannis

答えて

1

、次のようにランダムウェイトを生成します。

だからあなたの問題は、ループの各反復であることである

for (int i = 0; i < numberOfPeople; i++) 
    data[i] = new BodyMassIndex(name, age, weight, height); 

createObjectsToFillArray(BodyMassIndex[] data, String name, int age, double weight, double height, int numberOfPeople) 

その後、数回ループ:

double weight = loadRandomWeights(a); 

あなたは、配列を埋めコードにこれを渡します同じランダム値を使用します。あなたはその配列の各エントリは異なる値を取得するループの内側に新しいランダム値を生成する必要があります

明らか
for (int i = 0; i < numberOfPeople; i++) { 
    double weight = loadRandomWeights(); 
    .... 
    data[i] = new BodyMassIndex(name, age, weight, height); 
} 

同じことが他の値に適用されます。ループ内で値を生成する必要があります。

あなたのprintArrayメソッドにも同じバグがあります。あなたが作成したオブジェクトからそれを読み取るのではなく、同じ値を再印刷します。その方法では、BodyMassIndexオブジェクトの値を読み取ります。

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; 
} 

これは単なる可能性:あなたはどちらへの書き込みや、あなたが渡す配列から読んでいるとしても

、私はあなたのloadRandomWeights()方法の他の同様のものを考えていないがdataパラメータを必要と

public static double generateRandomWeight() { 
    double min = 100; 
    double max = 300; 
    return randomInt(min, max); 
} 
+0

for(int i = 0; i user1309594

+0

申し訳ありませんが、括弧を忘れてしまいました。今修正されました。 –

+0

おそらく私はちょうどばかだ...私はこれをどうやって得たのか分からない。私はこれまでのところ不満を超えていますが、それは面白くないものです。あなたの for(int i = 0; i user1309594

0

ループのためのE-RACHのために試してみてください。

for (int row = 0; row < data.length; row++) { 
    double weight = randomInt(min, max); 

    return weight; 


    } 
} 

については、ループのためのE-RACHのために試してみてください。

例えば体重を見てみると
for (int row = 0; row < data.length; row++) { 
    double weight = randomInt(min, max); 


    } 
    return weight; 


} 
+0

私はこれを試しましたが、return文が見えないというエラーが表示されます。そして、私がreturn文を最初のものの外に移動すると、変数の重みが何であるかがわかりません。 – user1309594

関連する問題