无法获得有效的数组输出

时间:2014-12-06 12:42:31

标签: java

我很抱歉提前代码很长 - 但是他们阅读和理解非常简单,因为我是初学者。

问题在于我试图获得此输出:

Black Image Constructor:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)

Constructor with RGBColor[][] Array Parameter:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(1,1,1) (1,1,1) (1,1,1) (1,1,1)
(2,2,2) (2,2,2) (2,2,2) (2,2,2)

而不是那样,我只是犯了错误。

以下是我的3个代码:

RGBColor类:

/**
* This program is used to represent 3 Colors: Red, Green, Blue. (RGB)
* These colors hold values between 0 and 255.
* 
* 
* @Author Ilan Aizelman.
*/
public class RGBColor {
        /**
         * attributes: red, green and blue component of a color.
         */
        private int _red,_green,_blue;

        /**
         * final variables.
         */
        private final int MAX_VALUE = 255,MIN_VALUE = 0;
        private final double THIRTY_PERCENT = 0.3,FIFTY_NINE_PERCENT = 0.59,ELEVEN_PERCENT = 0.11;                   

        /**
         * Consctructor which gets 3 colors (RGB), we check here if their range is valid (0 - 255), if not we assign black to it.
         *
         *  @param red - The red color component value.
         *  @param green - The green color component value.
         *  @param blue - The blue color component value
         */
        public RGBColor(int red, int green, int blue)
        {
            if(isValid(red,green,blue))
            {
                _red   = red;
                _green = green;
                _blue  = blue;
            }
            else
                doBlack();
        }


        /**
         * Construct a black RGBColor. i.e. red = green = blue = 0
         */
        public RGBColor()
        {
        doBlack();
    }



    /**
     * Here we check if the color number was entered correctly.
     * It has to be an integer (whole number) between 0-255.
     * 
     * @param nums - a component value, should be the number between 1-4
     * @param return - return true if the number is between 1-4, false otherwise.
     */
    private boolean isValid(int nums)
    {
        return ((nums >= MIN_VALUE) && (nums <= MAX_VALUE));
    }

    /**
     * Here we check if the color number was entered correctly.
     * It has to be an integer (whole number) between 0-255.
     * 
     * @param red - the red component
     * @param green - the green component
     * @param blue - the red component
     * @param return true if values are correct, false otherwise.
     */
    private boolean isValid(int red, int green, int blue)
    {
        return ((red <= MAX_VALUE && red >= MIN_VALUE && 
                green <= MAX_VALUE && green >= MIN_VALUE &&
                blue <= MAX_VALUE && blue >= MIN_VALUE));
    }
    /**
     * Returns RGB color string triplet with numbers between 0-255, i.e. (0,127,127)
     */
    public String toString()
    {
        return ("(" + _red + "," + _green + "," + _blue + ")");
    }

    /**
     * RGBColor will become the color Black. (0,0,0)
     */
    private void doBlack()
    {
        _red = _green = _blue = 0;
    }

}

RGBImage类:

    public class RGBImage
    {

    private int _rows, _cols;

    final int ZERO_VALUE = 0;



     private RGBColor[][] _pixels;

        /**
         * Constructor for objects of class RGBImage
         */
        public RGBImage(int rows, int cols)
        {
          _rows = rows;
          _cols = cols;
          _pixels = new RGBColor[_rows][_cols];
          for(int i = 0; i < _rows; i++)
          {
            for(int j = 0; j < _cols; j++)
            {
                _pixels[i][j] = new RGBColor();
            }
                //System.out.println();  
      }
    }

    public RGBImage(RGBColor[][] pixels)
    {
        _pixels = new RGBColor[pixels.length][pixels[0].length];
        for(int i = 0; i < pixels.length; i++)
        {
            for(int j 0; j < pixels[0].length; j++)
            {
                _pixels[i][j] = new RGBColor(pixels[i][j]);
            }
        }
    }
}

和我的StudentTester类必须打印我在开头写的输出:

public class StudentTester {

    public static void main(String[] args) {

        System.out.println("Black Image Constructor:");
        RGBImage rgbImg0 = new RGBImage(3,4);       
        System.out.println(rgbImg0);    

        System.out.println("Constructor with RGBColor[][] Array Parameter:");
        RGBColor[][] rgbArray1 = new RGBColor[3][4];
        for (int i=0; i<rgbArray1.length;i++)
            for (int j=0; j<rgbArray1[0].length;j++)    
                rgbArray1[i][j] = new RGBColor(i,i,i);                      
        RGBImage rgbImg1 = new RGBImage(rgbArray1);
        System.out.println(rgbImg1);


        System.out.println("Have a Nice Work!");
    }
}

错误是:

Black Image Constructor:
RGBImage@47520d
Constructor with RGBColor[][] Array Parameter:
RGBImage@1aa4b35
Have a Nice Work!

编辑:

我现在已经添加了这个,

I've added this:

public String toString() {
        String pixelSet="";
        for(int i=0; i<_rows;i++){
            for(int j=0 ;j<_cols;j++){
                pixelSet+=this._pixels[i][j].toString();
            }
            pixelSet +="\n";
        }
        return pixelSet;
    }

我没有得到&#34;构造函数与RGBColor [] []数组参数:&#34;输出了。还在努力,谢谢你们!

解决方案:(edit2)

public RGBImage(RGBColor[][] pixels)
{
    _rows = pixels.length;
    _cols = pixels[0].length;
    _pixels = new RGBColor[_rows][_cols];
    for(int i = 0; i < _rows; i++)
    {
        for(int j = 0; j < _cols; j++)
        {
            _pixels[i][j] = new RGBColor(pixels[i][j]);
        }
    }
}

3 个答案:

答案 0 :(得分:2)

在这里打印一个物体。

System.out.println(rgbImg0);

但是你没有定义打印RGBImage对象时想要发生的事情。将对象引用传递给print语句时,将调用对象toString();方法并打印出结果。默认情况下,Object子类会打印出一个引用,例如RGBImage@47520d,即ClassName + @ + Hashcode

您可以覆盖toString()的{​​{1}}方法来打印您需要的内容。

RGBColor

然后,您可以覆盖@Override public String toString() { return "("+_red+","+_green+","+_blue+")"; } 类的toString()方法,以打印RGBImage个对象的数组。

RBGColor

我希望这会有所帮助。

答案 1 :(得分:2)

你好@Illan Aizelman WS,

我没有在代码中看到任何编译错误,但是看看输出我可以看到你得到的是你要打印的对象的toString表示。如果您想获得问题中指明的输出,只需覆盖toStringjava.lang.Object类的RGBImage方法即可。如果您直接尝试使用System.out.printXX打印对象,则无法获得所需的输出。请参阅以下步骤。

  1. 覆盖toString课程中的RGBImage方法。
  2. 使用toString方法迭代数组。
  3. 在遍历数组时,打印您遇到的每个元素并应用您希望的格式。

答案 2 :(得分:0)

您还必须在RgbImage类中实现toString(类似于您的RgbColor类)。标准实现只是打印RgbImage实例的objectId。
想想如果打印RgbImage实例会发生什么。例如,您可以调用该图像的所有单个像素的toString,您可以应用一些额外的格式等等。

相关问题