2014-12-06 43 views
2

我真的很抱歉,代码很长 - 但它们非常易于阅读和理解,因为我是初学者。无法获得有效的数组输出

的问题是,我试图让这个输出:

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: 
[email protected] 
Constructor with RGBColor[][] Array Parameter: 
[email protected] 
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; 
    } 

,我不RGBColor [] []数组参数获得“构造:“输出。仍在努力,谢谢你们!

溶液:(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]); 
     } 
    } 
} 
+4

请包括什么是错误?什么是输出? – 2014-12-06 12:44:41

+6

不是为长代码道歉,如果您要创建一个能够演示问题的“short *”示例会更有帮助 - 尝试一次删除一块代码,直到您解决问题,或者您有一个简短的例子。通常你会发现,想出一个简短而完整的程序就是你自己找到问题所需要的一切。 – 2014-12-06 12:46:21

+1

[This](http://stackoverflow.com/help/mcve)将帮助您编写一个易于回答的最小,完整,可验证的示例。 – FlyingPiMonster 2014-12-06 12:49:49

回答

1

这里要打印的对象。

System.out.println(rgbImg0); 

但是,您尚未定义打印RGBImage对象时想要发生的情况。当您将对象引用传递给打印语句时,将调用对象toString();方法并打印出结果。默认Object子类打印出一个参考,如[email protected]这是ClassName + @ + Hashcode

您可以覆盖toString()方法RGBColor来打印您所需的内容。

@Override 
public String toString() { 
    return "("+_red+","+_green+","+_blue+")"; 
} 

然后你就可以覆盖RGBImage类的toString()方法来打印RBGColor对象数组。

public String toString() { 
    // If your not too bothered about formatting, you can do it in a single line. 
    // return Arrays.deepToString(_pixels); 
    String pixelSet=""; 
    for(int i=0; i<_pixels.length;i++){ 
     for(int j=0 ;j<_pixels[i].length;j++){ 
      pixelSet+=this._pixels[i][j].toString(); 
     } 
     pixelSet +="\n"; 
    } 
    return pixelSet; 
} 

我希望这会有所帮助。

+0

仍然无法获得输出的第二部分。我用我的toString代码编辑了这篇文章。 – 2014-12-06 13:09:45

+0

仍然无法获得第二个输出。 :( – 2014-12-06 13:23:09

+0

@IlanAizelmanWS - 我认为这可能与你的'公共RGBImage(RGBColor [] []像素)'构造函数有关,因为你不改变'_row'&'_cols'。我已经编辑了一个新版本为您的RGBImage'toString()'方法应该工作。 – 2014-12-06 13:30:51

0

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

1

嗨@Illan Aizelman WS,

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

  1. 覆盖toString方法RGBImage类。
  2. toString方法中遍历数组。
  3. 在遍历数组时,打印遇到的每个元素并应用所需的格式。