2012-04-03 313 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()也不执行任何已选择年龄的事件。 – John3136 2012-04-03 06:00:22

回答

0
  • 您需要标记public double getBodyMassIndex()static - 你并不真正需要的BodyMassIndex实例调用该方法,因为你不使用内部的任何成员数据。

所以,这种方法的签名应该是:

public static double getBodyMassIndex() {...} 
  • 的标准模式得到整数内部的一些范围是:

    敏+(INT)(的Math.random ()*((Max-Min)+ 1))

java Math libra ry函数Math.random()在[0,1)范围内生成一个double值。请注意这个范围不包括1

为了得到价值首先你需要通过值的你想要覆盖的范围的大小相乘的特定范围。

Math.random() * (Max - Min) 

这将返回[0,Max-Min)范围内的值。 如果你想[5,10]你需要支付5个整数值,所以你使用:

Math.random() * 5 

这范围内的返回值[0,5)

现在你需要转移这个范围内,对您的目标范围内。您可以通过添加最小值来完成此操作。

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

现在,您将在范围[Min,Max)得到的值。在我们的例子,这意味着[5,10)

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

但是,这还不包括Max和你得到的双重价值。为了获得最大值,你需要在范围参数(Max - Min)中加1,然后通过强制转换为整数来截断小数部分。这是通过:

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

而你有它。范围[Min,Max]随机整数值,或每例子[5,10]

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

here

好运被偷了!

+0

谢谢soooo多!我修正了这一点。一件如此简单的事情令人沮丧。现在有什么想法,为什么我得到每个条目相同的名称,年龄,体重等? – user1309594 2012-04-03 06:11:40

+0

@ user1309594,请参阅我的编辑 – aviad 2012-04-03 06:24:57

-1

您需要BodyMassIndex的一个实例:

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

你使用它,方式意味着,方法声明为BMI类静态的。像:

public static double getBodyMassIndex() {...} 

但是这不会是一个伟大的设计。

+0

调用静态方法时不需要'BodyMassIndex'的实例。 – aviad 2012-04-03 06:03:42

+0

是的,我的意思是有两种方法调用getBodyMassIndex()。作为一个实例 - 具有显式实例的方法或者像他已经尝试过的静态方法,但是该方法必须是静态的。 – boskop 2012-04-03 06:36:37

+0

顺便说一句,如果方法是'静态','bmi'甚至不需要实例化:'BodyMassIndex bmi = null;'稍后调用到'bmi.getBodyMassIndex()'仍然可以工作(检查这个:) – aviad 2012-04-03 06:59:42

0

是的,你不能访问从您的课BodyMassIndex.getBodyMassIndex();BodyMassIndex.getStatus();称为ClassLab3B,因为这两种方法不是静态的..

如果你让他们静,U将无法访问乌尔班BodyMassIndex的非静态成员..

,这样你们可以返回BodyMassIndex []从createObjectsToFillArray(),然后访问每个实例在一个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(); String status = BodyMassIndex。 getStatus();

在这2种方法中做了什么 你已经创建了2种方法作为非静态,但使用类名称调用。如果你将这两个方法创建为静态,那么在这2个方法中你也使用了非静态变量。

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

对(INT I = 0;我< = numberOfPeople-1; i ++在){

BMI [I] = A [1] .getBodyMassIndex();

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

}

公共静态无效printArray(BodyMassIndex []数据,字符串名称,诠释年龄,双体重,双高,双[] BMI,字符串[]状态)