如何向对象添加值

时间:2022-04-05 19:40:55

I try to learn Java and have a question. I created a class Color. This class contains an constructor for "color" with 3 int values (red, green, blue). Then I have an array with a lot of color elements.

我尝试学习Java并有一个问题。我创建了一个类Color。此类包含“color”的构造函数,其中包含3个int值(红色,绿色,蓝色)。然后我有一个包含很多颜色元素的数组。

Now I want to add 4 of this elements to one and divide it, so I get the average of each int value.

现在我想将4个元素添加到一个并除以它,因此我得到每个int值的平均值。

But eclipse says, that the operator + is undefined.

但是eclipse说,运算符+是未定义的。

Color sum = new Color(red, green, blue)
for (int i = 0; i < length; i ++) {
     sum = sum + array[i];
}

public Color(int r, int g, int b){
    this.red=r;
    this.green=g;
    this.blue=b;
}

How can I add the values of each array element to a sum? The elements in the array are from the type color.

如何将每个数组元素的值添加到总和中?数组中的元素来自类型颜色。

4 个解决方案

#1


2  

There is no operator overloading in Java. You'd have to handle each value separately:

Java中没有运算符重载。您必须分别处理每个值:

int avgRed = 0;
int avgGreen = 0;
int avgBlue = 0;
for (int i = 0; i < length; i ++) {
    avgRed += array[i].getRed();
    avgBlue += array[i].getBlue();
    avgGreen += array[i].getGreen();
}
Color avgColor = new Color(avgRed / length, avgBlue / length, avgGreen / length);

#2


1  

sum variable is a Color and the concatenation in the form

sum变量是一种颜色和表格中的连接

sum = sum + array[i];

is not defined, so the compiler can not understand how to resolve such operation

没有定义,所以编译器无法理解如何解决此类操作

you could maybe consider something like

你可能会考虑类似的东西

sum.red +=  array[i];

is array[i] is holding an integer or:

是array [i]持有一个整数或:

sum.red +=  array[i].red;

if the array is an array of colors

如果数组是一个颜色数组

#3


0  

Sum is an object not variable.You can add value to its associated parameter.

Sum是一个不可变的对象。您可以为其关联参数添加值。

for example :

例如 :

sum.red = sum.red+array[i].red

#4


0  

You can't use the + operator to do what you have in mind, but you could add an add method to your Color class so that you could then write:

您不能使用+运算符来实现您的想法,但您可以向Color类添加一个add方法,以便您可以编写:

Color sum = new Color(red, green, blue)
 for (int i = 0; i < length; i ++) {
    sum = sum.add(array[i]);

Let's assume for this discussion that adding Colors means we add the red part of the one to the red part of the other, green to green, blue to blue. Your add method would look like this:

让我们假设在这个讨论中添加颜色意味着我们将红色部分添加到另一个的红色部分,绿色到绿色,蓝色到蓝色。你的add方法如下所示:

    public Color add(Color other) {
        return new Color(this.red + other.red, 
                         this.green + other.green,
                         this.blue + this.blue);
    }

#1


2  

There is no operator overloading in Java. You'd have to handle each value separately:

Java中没有运算符重载。您必须分别处理每个值:

int avgRed = 0;
int avgGreen = 0;
int avgBlue = 0;
for (int i = 0; i < length; i ++) {
    avgRed += array[i].getRed();
    avgBlue += array[i].getBlue();
    avgGreen += array[i].getGreen();
}
Color avgColor = new Color(avgRed / length, avgBlue / length, avgGreen / length);

#2


1  

sum variable is a Color and the concatenation in the form

sum变量是一种颜色和表格中的连接

sum = sum + array[i];

is not defined, so the compiler can not understand how to resolve such operation

没有定义,所以编译器无法理解如何解决此类操作

you could maybe consider something like

你可能会考虑类似的东西

sum.red +=  array[i];

is array[i] is holding an integer or:

是array [i]持有一个整数或:

sum.red +=  array[i].red;

if the array is an array of colors

如果数组是一个颜色数组

#3


0  

Sum is an object not variable.You can add value to its associated parameter.

Sum是一个不可变的对象。您可以为其关联参数添加值。

for example :

例如 :

sum.red = sum.red+array[i].red

#4


0  

You can't use the + operator to do what you have in mind, but you could add an add method to your Color class so that you could then write:

您不能使用+运算符来实现您的想法,但您可以向Color类添加一个add方法,以便您可以编写:

Color sum = new Color(red, green, blue)
 for (int i = 0; i < length; i ++) {
    sum = sum.add(array[i]);

Let's assume for this discussion that adding Colors means we add the red part of the one to the red part of the other, green to green, blue to blue. Your add method would look like this:

让我们假设在这个讨论中添加颜色意味着我们将红色部分添加到另一个的红色部分,绿色到绿色,蓝色到蓝色。你的add方法如下所示:

    public Color add(Color other) {
        return new Color(this.red + other.red, 
                         this.green + other.green,
                         this.blue + this.blue);
    }