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)));
}
}
}
}
编写一个程序,以厘米为单位读取一系列人的高度。我必须输出所有那些高于平均身高的人的高度。我做了一种计算平均值的方法和另一种做法,如果你能帮助我。我必须使用数组做到这一点,因此对于第三种方法,我试图制作一个高度超过其平均高度的玩家图表
答案 0 :(得分:2)
public static double calcAverage(double heights)
您的方法需要double
参数,而不是double[]
。因此,您无法将double array
传递给方法。
第二种方法:public static double heightAverage(double average)
需要一个返回值(double
),但你没有返回任何内容。
答案 1 :(得分:0)
有几个错误
public static double calcAverage(double heights)
您通过了double
而不是double[]
忘了初始化数组
String players[]=new String[10];
添加了int和double值
double sum = 0;
for (int i=0; i < heights.length; i++)
{
sum = sum + heights[i];
}
你的方法public static double heightAverage(double[] average)
完全令人困惑
答案 2 :(得分:0)
既然你明确地问过,我来看看帖子是什么,但我不明白你在做什么。我假设某些内容并共享固定的代码片段,希望它可以帮助您自己编写代码以获得所需内容。
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)));
}
}
}
输出:
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