2012-04-03 78 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

您是否尝试过调试代码? – 2012-04-03 06:22:40

+1

你并不需要那么多评论^^。 – Giannis 2012-04-03 13:03:06

回答

1

看重量。例如,您生成一个随机重量如下:

double weight = loadRandomWeights(a); 

然后,您可以通过这个来填充数组代码:

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

所以你的问题是,每次循环是使用相同的随机值。你需要循环所以阵列中的每个条目都有不同的值内生成一个新的随机值

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

显然同样适用于其他值;你也需要在循环中为它们生成值。

而你在printArray方法中有同样的错误。它重新打印相同的值,而不是从您创建的对象中读取它。在该方法中,读取BodyMassIndex对象的值。

另外,我不认为你的loadRandomWeights()方法其他类似的人需要data参数作为你既不写入或从您在传递数组阅读:

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 generateRandomWeight() { 
    double min = 100; 
    double max = 300; 
    return randomInt(min, max); 
} 
+0

for(int i = 0; i user1309594 2012-04-03 06:49:07

+0

对不起,忘了括号。现在修复。 – 2012-04-03 06:57:07

+0

也许我只是一个白痴...我不知道我已经得到了这么多。我非常沮丧,甚至不好笑。你的 for(int i = 0; i user1309594 2012-04-03 07:00:58

0

试用erach for循环:

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

    return weight; 


    } 
} 

什么

试用erach for循环:

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


    } 
    return weight; 


} 
+0

我试过这个,但后来我得到一个错误,说它看不到返回语句。如果我将return语句移到第一个}之外,那么它不知道变量的重量是多少。 – user1309594 2012-04-03 06:43:25