无法返回对象

时间:2015-01-30 15:26:31

标签: java object for-loop

这是我的计划:

Public class SimpleRGB
{
    //Instance variables

    private int width, height;
    private int[][] red = new int[10][10];
    //same for green/blue

    public SimpleRGB(int aWidth, int aHeight)
    {
        //...
    }

    //Some methods

这是给我带来麻烦的方法。在这个方法中我应该创建一个新的图像(简单的rgb对象),使用嵌套的for循环将新的简单rgb的红色D数组设置为这个简单的rgb的红色2D数组(不确定我是否有& #39;正确),在同一个循环中将绿色和蓝色设置为0(想想我已经正确完成)然后返回新的简单rgb对象。我对此非常陌生,所以我不确定如何返回我的简单对象也会返回我在for-loop中设置的数据,同时我也不相信我正在创建/正确返回简单的rgb。任何帮助将不胜感激!谢谢。

    Public SimpleRGB getRedImage()
    {

        SimpleRGB redImage = new SimpleRGB(); //Completely lost here. Not sure why but Java won't let me use "new: here. Also I'm confused as to how the data from the for-loop will even bee associated with this objectd       

        for (int i = 0; i < width; i++)
        {
            For (int j  0; j < height; j++)
            {
                red[i][j] = ?? //some code here to set red value of new object to red value
                green[i][j] = 0; //set to 0 b/c we only want red color
                blue[i][j] = 0; //set to 0 b/c we only want red color
            }
        }

        Return redImage(); //not sure if this is correct/if "()" are needed

1 个答案:

答案 0 :(得分:0)

以下是您的方法的一些快速修复。首先,您需要在SimpleRGB()调用中添加两个int参数,即SimpleRGB(int,int)秒我在for循环中针对该新对象的数组。

Public SimpleRGB getRedImage()
{

    SimpleRGB redImage = new SimpleRGB(int,int);     

    for (int i = 0; i < width; i++)
    {
        For (int j  0; j < height; j++)
        {
            redImage.red[i][j] = ??;  
            redImage.green[i][j] = 0;
            redImage.blue[i][j] = 0;
        }
    }

    Return redImage; //removed the ()
}