首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >需要Java帮助

需要Java帮助
EN

Stack Overflow用户
提问于 2012-04-03 13:46:20
回答 4查看 1.1K关注 0票数 0

好吧,我已经非常接近解决方案了,但是我就是想不出来。由于某种原因,我的代码没有生成随机的年龄、高度、体重等。此外,我收到一个错误“无法从静态上下文行43和45引用非静态方法getBodyMassIndex()。我做了一些研究并读到这个错误是因为我需要创建一个实例来应用它,但我迷路了。我以为我已经这样做了,但我什么也做不到。这基本上是一个自学的java课程,我真的很努力。任何帮助都非常感谢!代码如下:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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;
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-04-03 13:59:44

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

因此,此方法的签名应为:

代码语言:javascript
复制
public static double getBodyMassIndex() {...}

  • 在某个范围内获取整数的标准模式是:

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

java Math库函数Math.random()在[0,1]范围内生成一个双精度值。请注意,此范围不包括1。

为了得到一个特定的值范围,首先需要乘以您想要覆盖的值范围的大小。

代码语言:javascript
复制
Math.random() * ( Max - Min )

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

代码语言:javascript
复制
Math.random() * 5

这将返回一个范围为[0,5)的值

现在,您需要将此范围向上移动到您的目标范围。您可以通过添加Min值来执行此操作。

代码语言:javascript
复制
Min + (Math.random() * (Max - Min))

现在,您将获得一个范围为[Min,Max)的值。按照我们的示例,这意味着[5,10)

代码语言:javascript
复制
5 + (Math.random() * (10 - 5))

但是,这仍然不包括最大值,并且您将获得双精度值。为了获得包含的最大值,您需要向范围参数(Max - Min)添加1,然后通过强制转换为int来截断小数部分。这可以通过以下方式实现:

代码语言:javascript
复制
Min + (int)(Math.random() * ((Max - Min) + 1))

现在你就知道了。范围[Min,Max]或示例[5,10]中的随机整数值

代码语言:javascript
复制
5 + (int)(Math.random() * ((10 - 5) + 1))

here窃取

祝好运!

票数 0
EN

Stack Overflow用户

发布于 2012-04-03 14:34:01

双BMI = BodyMassIndex.getBodyMassIndex();字符串状态= BodyMassIndex.getStatus()

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

double []BMI=new doublenumberOfPeople;

String []status=new StringnumberOfPeople;

for(int i=0;i<=numberOfPeople 1;i++){

BMIi=ai.getBodyMassIndex();

statusi=ai.getStatus();

}

公共静态空printArray(BodyMassIndex[]数据,字符串名,整数年龄,双倍体重,双高,double[] BMI,String[]状态)

票数 1
EN

Stack Overflow用户

发布于 2012-04-03 14:09:01

是的,您不能从名为ClassLab3B的类访问BodyMassIndex.getBodyMassIndex();BodyMassIndex.getStatus();,因为这两个方法不是静态的。

如果你把它们设为静态的,你将不能访问你的类BodyMassIndex的非静态成员。

因此,您可以从createObjectsToFillArray()返回BodyMassIndex[],然后在for循环中访问每个实例并调用printArray()

像这样的东西

代码语言:javascript
复制
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);
        }

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9987723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档