简体   繁体   English

使用数组查找平均高度

[英]Using Array to find average height

public static void main (String args[])
{
    //10 name arrays
    String players[];
    players[0]="Kevin Love";
    players[1]="Kyrie Irving";
    players[2]="Lebron James";
    players[3]="Dion Waiters";
    players[4]="Shawn Marion";
    players[5]="Tristan Thompson";
    players[6]="Anderson Varejo";
    players[7]="Joe Harris";
    players[8]= "Mike Miller";
    players[9] = "Brendan Haywood";
    //10 height arrays in centimeter
    double heights[];
    heights[0]=208;
    heights[1]=191;
    heights[2]=203;
    heights[3]=193;
    heights[4]=201;
    heights[5]=206;
    heights[6]=208;
    heights[7]=198;
    heights[8]=203;
    heights[9]=213;
    //String c= calcAverage(heights);
    //System.out.println("The average of your arrays numbers is: " + c);
}
// for calculating average for heights
public static double calcAverage(double heights) {
    int sum = 0;
    for (int i=0; i < heights.length; i++)
    {
        sum = sum + heights[i];
    }
    double average = sum / (double)heights.length;
    return average;
}
// height more than average
public static double heightAverage(double average)
{
    String heights;
    if (heights>average)
    {
        System.out.println("Players|Heights");
        System.out.println("-----------+-----------");
        DecimalFormat df = new DecimalFormat("#.##");
        for (double c = 1; c <= 45; c += 0.5) {
            double d = (c * 0.381);
            System.out.println(String.format("%-11s|%8s", c,   df.format(d)));
        }
    }
}

} }

Write a program to read in a series of heights of people in centimeters. 编写程序以读取一系列以厘米为单位的人的身高。 I have to output the heights of all those that are above average in height for the group. 我必须输出该组中所有高于平均高度的高度。 I did one method for calculating average and another method for doing if statements can u please help me. 我做了一种计算平均值的方法,另一种做了陈述是否可以帮助我的方法。 I have to do this using arrays, therefore for the third method i am trying to make a chart of players with the heights that are above their average height 我必须使用数组来执行此操作,因此对于第三种方法,我试图制作一个高度超过其平均身高的球员图表

public static double calcAverage(double heights)

your method is expecting a double parameter, not double[] . 您的方法需要一个double参数,而不是double[] So you cannot pass your double array to the method. 因此,您无法将double array传递给该方法。

The 2nd method: public static double heightAverage(double average) needs a return value( double ), but you didn't return anything. public static double heightAverage(double average)方法: public static double heightAverage(double average)需要一个返回值( double ),但是您什么也没有返回。

in you class there are several errors 在你的课上有几个错误

public static double calcAverage(double heights)

you passed double instead of double[] 您通过了double而不是double[]

forgot to initialize the array 忘记初始化数组

String players[]=new String[10];

Added int and double values 添加了int和double值

double sum = 0;
    for (int i=0; i < heights.length; i++)
    {
        sum = sum + heights[i];
    }

your method public static double heightAverage(double[] average) totally confusing 您的方法public static double heightAverage(double[] average)完全令人困惑

Since you explicitly asked, I came to see whats the post, but I don't understand what you are up to. 自从您明确提出问题以来,我来看看帖子是什么,但是我不明白您在做什么。 I assume something and sharing the fixed snippet, hoping it might help you to code yourself to get what you need. 我假设有什么事情,并分享了固定的代码片段,希望它可以帮助您编写代码以获取所需的内容。

import java.text.DecimalFormat;

public class Test1 {
    public static void main(String args[]) {
        // 10 name arrays
        String players[] = new String[10];
        players[0] = "Kevin Love";
        players[1] = "Kyrie Irving";
        players[2] = "Lebron James";
        players[3] = "Dion Waiters";
        players[4] = "Shawn Marion";
        players[5] = "Tristan Thompson";
        players[6] = "Anderson Varejo";
        players[7] = "Joe Harris";
        players[8] = "Mike Miller";
        players[9] = "Brendan Haywood";
        // 10 height arrays in centimeter
        double heights[] = new double[10];
        heights[0] = 208;
        heights[1] = 191;
        heights[2] = 203;
        heights[3] = 193;
        heights[4] = 201;
        heights[5] = 206;
        heights[6] = 208;
        heights[7] = 198;
        heights[8] = 203;
        heights[9] = 213;


        double average = 203; // you have to calculate average first!

        System.out.println("print all players with their heights");
        System.out.println("Players|Heights");
        System.out.println("---------+---------");
        for(int i=0;i<10;i++){
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(String.format("%-11s|%8s", players[i],
                    df.format(heights[i])));
        }

        System.out.println("****************************");
        System.out.println("****************************");


        System.out.println("print only those players whose height is greater than average");
        System.out.println("Players|Heights");
        System.out.println("---------+---------");
        for(int i=0;i<10;i++){
            printPlayerheights(average, heights[i], players[i]);
        }

    }

    public static void printPlayerheights(double average, double height,
            String player) {
        if (height > average) {
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(String.format("%-11s|%8s", player,
                    df.format(height)));
        }
    }
}

Output: 输出:

print all players with their heights

Players|Heights

---------+--------- --------- + ---------

Kevin Love |     208
Kyrie Irving|     191
Lebron James|     203
Dion Waiters|     193
Shawn Marion|     201
Tristan Thompson|     206
Anderson Varejo|     208
Joe Harris |     198
Mike Miller|     203
Brendan Haywood|     213


print only those players whose height is greater than average

Players|Heights

---------+--------- --------- + ---------

Kevin Love |     208
Tristan Thompson|     206
Anderson Varejo|     208
Brendan Haywood|     213

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM