我正在尝试创建一个计算平均值的void方法,但我想将值从main方法传递给方法。这是我想要做的一个例子:
public class Plant {
int leaves;
int age;
int sumLeaves;
double average;
void averageLeaves() {
sumLeaves = leaves + leaves; //here is where I need help
average = (double) sumLeaves / 2;
System.out.println("The average number of leaves is: " + average);
}
public static void main(String[] args) {
Plant plantA = new Plant();
plantA.leaves = 5;
Plant plantB = new Plant();
plantB.leaves = 3;
averageLeaves();
}
我遇到的问题是将多种植物的价值纳入方法中。我尝试过使用.get
,循环,static
以及许多其他内容,但还没有想到它。我想要的是plantA.leaves
和plantB.leaves
对方法的价值,以便找到它们的总和。
答案 0 :(得分:4)
由于您在Plant类中定义了averageLeaves()
,因此您必须实际从其中一个工厂实例调用方法。这是你应该怎么做的。
plantA.averageLeaves();
你在这里犯的一个重大错误是,
sumLeaves = leaves + leaves;
这实际上添加了特定(一个)植物实例的叶子,这是错误的。你必须实际传递差异实例中的叶子数。
使用getter& amp ;;这是一个更好的方法。 setter方法。将averageLeaves()
方法设为静态也很有意义。这样您就不需要实例来调用该方法。
public class Plant {
int leaves;
int age;
//int sumLeaves; you do not need them now, as the averageLeaves method is static
//double average;
static void averageLeaves (int leaves1, int leaves2) {
int sumLeaves = leaves2 + leaves1; //here is where I need help
double average = (double) sumLeaves / 2;
System.out.println("The average number of leaves is: " + average);
}
void setLeaves(int leaves){
this.leaves = leaves;
}
int getLeaves(){
return this.leaves;
}
}
public class Main {
public static void main(String[] args) {
Plant plantA = new Plant();
plantA.setLeaves(5);
Plant plantB = new Plant();
plantB.setLeaves(3);
Plant.averageLeaves(plantA.getLeaves(), plantB.getLeaves());
}
}
答案 1 :(得分:0)
你必须这样做:
public class Plant
{
int age;
int sumLeaves;
double average;
void averageLeaves (int leaves)
{
sumLeaves = leaves + leaves; //here is where I need help
average = (double) sumLeaves / 2;
System.out.println("The average number of leaves is: " + average);
}
}
public class Example
{
public static void main(String[] args)
{
Plant plantA = new Plant();
plantA.leaves = 5;
Plant plantB = new Plant();
plantB.leaves = 3;
plantA.averageLeaves(plantA.leaves);
plantB.averageLeaves(plantB.leaves);
}
}
看看你只需要声明要输入到方法中的变量(在括号内),当你使用你的方法时,你必须传递方法将使用的值(按声明的顺序)在方法)。
我希望它会对你有所帮助!
答案 2 :(得分:0)
有点不清楚你的意思。植物本身的平均值是多少?这两种植物的平均值。我假设后者。然后,一种方法是将方法作为静态成员调用,并将叶子作为参数传递。
public class Plant {
int leaves;
static void averageLeaves (int leaves1, int leaves2) {
int sumLeaves = leaves1 + leaves2; //here is where I need help
double average = (double) sumLeaves / 2;
System.out.println("The average number of leaves is: " + average);
}
public static void main(String[] args) {
Plant plantA = new Plant();
plantA.leaves = 5;
Plant plantB = new Plant();
plantB.leaves = 3;
Plant.averageLeaves(plantA.leaves, plantB.leaves);
}
答案 3 :(得分:0)
这是一个简单版本,允许您传入可变数量的植物并计算它们的平均值。
public class Plant {
private int leaves;
public Plant(int leaves) {
this.leaves = leaves;
}
public int getLeaves() {
return this.leaves;
}
public static void main(String[] args) {
PrintLeavesAverage(
new Plant(5),
new Plant(10),
new Plant(15),
new Plant(12)
);
}
private static void PrintLeavesAverage(Plant... plants) {
int totalLeaves = 0;
for (Plant p : plants) {
totalLeaves += p.getLeaves();
}
double average = (double)totalLeaves/plants.length;
System.out.println("The average number of leaves is: " + average);
}
}
...或者如果你想获得更多的爱好者,并想尝试一些较新的Java 8功能,你可以使用流来计算平均值,如下所示:
private static void PrintLeavesAverage(Plant... plants) {
OptionalDouble average = Arrays.stream(plants).mapToInt(p -> p.leaves).average();
if (average.isPresent()) {
System.out.println("The average number of leaves is: " + average.getAsDouble());
} else {
System.out.println("No plants, no average");
}
}