简体   繁体   English

为什么进行循环打印会出现意外结果?

[英]Why is for loop printing unexpected results?

I am scanning a black and white photo that is 100x100 pixels, trying to print every x and y coordinate (pixel by pixel) based on it being black or white. 我正在扫描100x100像素的黑白照片,试图基于黑白坐标来打印每个x和y坐标(逐像素)。 As the print statement is running, I can see the expected result in the console, but once it finishes, it begins producing a value of 0 for every outcome value and only prints north of x 95. Can you please explain how to fix this? 随着print语句的运行,我可以在控制台中看到预期的结果,但是一旦完成,它将开始为每个结果值生成一个值为0的值,并且仅打印x x以北的值。您能解释一下如何解决此问题吗?

public class BW
{

public static void main(String[] args) {

try {    

BufferedImage original = ImageIO.read(new File("maze.jpg"));
int red;

int[][] outcome = new int[original.getHeight()][original.getWidth()];
for(int i=0; i<original.getHeight(); i++)
{
for(int j=0; j<original.getWidth(); j++)
  {
red = new Color(original.getRGB(i, j)).getRed();

int pixel = 0;
if(red > 1)
{
outcome[i][j] = 255;
System.out.println("x: " + i + " y: " + j + " value: " + outcome[i][j]);
} else {
   outcome[i][j] = 0;
   System.out.println("x: " + i + " y: " + j + " value: " + outcome[i][j]);
} 
}
}

   } catch (IOException e) {e.printStackTrace();}
} 
}

The terminal output is below (it continues until an x of 99 and y if 99, with the y incrementing by 1, and the x switching from 98 to 99 halfway through- 终端输出低于此值(它一直持续到x为99且y为99为止,y递增1,并且x在中途从98切换到99,

产量

This is a screenshot I took while it was still running, where you can see it was calculating correctly. 这是我在其仍在运行时拍摄的屏幕截图,您可以在其中看到其计算正确。 Once completed, it turns into the image above. 完成后,它变成上面的图像。

在跑步的时候

Change 更改

for(int i=0; i<original.getWidth(); i++)
{
for(int j=0; j<original.getHeight(); j++)
{

to

for(int i=0; i<original.getHeight(); i++)
{
   for(int j=0; j<original.getWidth(); j++)
   {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM