2015-04-01 63 views
1

我需要顯示所選圖像中每個像素的所有RGB數字,每個數字用空格分隔,並在圖像的每一行之後使用println(break)。我已經想出並編寫了代碼來返回所有的RGB數字,但我不知道如何在每行之後分解。這是到目前爲止我的代碼..顯示圖像的RGB數字

public void getColor() 
{ 
    System.out.println("This picture's dimensions are: "+this.getWidth()+" by "+this.getHeight()); 
    for (int row=0; row < this.getHeight(); row++) // gives the number of rows 
    { 
    for (int col=0; col < this.getWidth(); col++)// gives the number of columns 
    { 
     Pixel pix = this.getPixel(col,row);   
     System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" "); 
    } // end of inner loop 
    }// end of outer loop 
} // end of method 

回答

2

你需要把換行最裏面的外面循環,因爲你希望它的每一行完成後運行。

public void getColor() 
{ 
    System.out.println("This picture's dimensions are: "+this.getWidth()+" by "+this.getHeight()); 
    for (int row=0; row < this.getHeight(); row++) // gives the number of rows 
    { 

    for (int col=0; col < this.getWidth(); col++)// gives the number of columns 
    { 
     Pixel pix = this.getPixel(col,row);   
     System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" "); 
    } // end of inner loop 

    //After this ^^^ for loop runs, you've gone over the whole row. 
    System.out.println(); 
    }// end of outer loop 
} // end of method 
0

只是由內環和外環之間插入print語句添加換行符每行的末尾。

public void getColor() 
{ 
    System.out.println("This picture's dimensions are: "+this.getWidth()+" by  "+this.getHeight()); 
    for (int row=0; row < this.getHeight(); row++) // gives the number of rows 
    { 
    for (int col=0; col < this.getWidth(); col++)// gives the number of columns 
    { 
     Pixel pix = this.getPixel(col,row);   
     System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" "); 
    } // end of inner loop 
    System.out.print("\n"); //added line break to end of line. 
    }// end of outer loop 
} // end of method 
+0

改用'System.out.println()'會更好 - 主要是因爲這實際上就是它的意義所在。 – 2015-04-01 00:43:53

+0

是的,雖然其他人已經用該解決方案做出了答案。因此,我覺得最好是把它作爲一種選擇,因爲它有許多其他閱讀可能會發現未來用途的應用程序。 – Danoram 2015-04-01 00:54:56

+0

啊,夠公平的。我沒有看到那一個。 – 2015-04-01 00:57:56