有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

显示图像的RGB编号的java

我需要显示所选图片中每个像素的所有RGB数字,每个数字用空格分隔,并在图片的每一行后使用println(break)。我已经找到并编写了返回所有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

共 (3) 个答案

  1. # 1 楼答案

    您需要将换行符放在最里面的for循环之外,因为您希望它在每行完成后运行

    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
    
  2. # 3 楼答案

    只要在内环和外环之间插入一条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