我真的很抱歉,代码很长 - 但它们非常易于阅读和理解,因为我是初学者。无法获得有效的数组输出
的问题是,我试图让这个输出:
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]);
}
}
}
请包括什么是错误?什么是输出? – 2014-12-06 12:44:41
不是为长代码道歉,如果您要创建一个能够演示问题的“short *”示例会更有帮助 - 尝试一次删除一块代码,直到您解决问题,或者您有一个简短的例子。通常你会发现,想出一个简短而完整的程序就是你自己找到问题所需要的一切。 – 2014-12-06 12:46:21
[This](http://stackoverflow.com/help/mcve)将帮助您编写一个易于回答的最小,完整,可验证的示例。 – FlyingPiMonster 2014-12-06 12:49:49