简体   繁体   English

为什么我的数组不满足这个 if 语句的条件?

[英]Why won't my array satisfy the conditions of this if-statement?

I'm doing this program for an assignment and I have absolutely zero idea why my array (reading values from a separate file) won't satisfy the conditions of this if-statement.我正在做这个程序来进行分配,我完全零知道为什么我的数组(从单独的文件中读取值)不满足这个 if 语句的条件。 For your convenience, the statement in question is located in the displayColonies class.为方便起见,有问题的语句位于 displayColonies class 中。

When i read in my array, it looks for the value at a specified row and column, which is an int value registering from 1-9 inclusive.当我读入我的数组时,它会在指定的行和列中查找值,这是一个从 1-9 注册的 int 值。 When I tested this periodically using print statements, the array does contain the correct value, but the if-statement is never activated.当我使用打印语句定期对此进行测试时,数组确实包含正确的值,但 if 语句从未被激活。 The conditions match with the array's value, but the statement is never evaluated, true or otherwise.条件与数组的值匹配,但从不评估语句,无论是否为true

Thanks for your help.谢谢你的帮助。

Code is attached below: DetectColonies2ElectricBoogaloo is client Slide is Object slide.dat is text file, arranged as follows下面附上代码:DetectColonies2ElectricBoogaloo是客户端Slide是Object slide.dat是文本文件,排列如下

6
8
10550000
00050000
00005500
01200000
01111000
00000030
public class DetectColonies2ElectricBoogaloo {

    public static void main(String [] args) {

        Slide culture = new Slide("Slide.dat");
        culture.displaySlide();
        culture.displayColonies();
   }
}
import java.io.*;

public class Slide {

    private char NON_COLONY = '0';
    private char [][] slideData;

    /**
     * constructor
     * pre: Slide file contains valid slide data in the format:
     * first line: lenght of slide
     * second line: width of slide
     * remaining lines: slide data
     * post: Slide data has been loaded from slide file.
     */
    public Slide(String s) {    
        try {
            File slideFile = new File(s);
            FileReader in = new FileReader(slideFile);
            BufferedReader readSlide = new BufferedReader(in);

            int length = Integer.parseInt(readSlide.readLine());
            int width = Integer.parseInt(readSlide.readLine());
            slideData = new char[length][width];

            for (int row = 0; row < length; row++) {
                for (int col = 0; col < width; col++) {
                    slideData[row][col] = (char)readSlide.read();
                }
                readSlide.readLine();   //read past end-of-line characters
            }
            readSlide.close();
            in.close();
       } catch (FileNotFoundException e) {
            System.out.println("File does not exist or could not be found.");
            System.err.println("FileNotFoundException: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Problem reading file.");
            System.err.println("IOException: " + e.getMessage());
        }
    }

    /**
     * Determines a colony size
     * pre: none
     * post: All colony cells adjoining and including cell (Row, Col) have 
     * been changed to NON_COLONY, and count of these cells is returned.
     */
    private int collectCells(int row, int col , char colour) {

        if ((row < 0) || (row >= slideData.length) || (col < 0) || (col >= slideData[0].length) || (slideData[row][col] != colour)) {
            return(0);
        } else {
            slideData[row][col] = NON_COLONY;
            return(1 + 
                collectCells(row + 1, col , colour) + 
                collectCells(row - 1, col , colour) + 
                collectCells(row, col + 1 , colour) + 
                collectCells(row, col - 1 , colour) + 
                collectCells(row - 1 , col - 1 , colour) + 
                collectCells(row + 1 , col + 1 , colour) + 
                collectCells(row - 1 , col + 1 , colour) + 
                collectCells(row + 1 , col - 1 , colour));
        }
    }

    /**
     * Analyzes a slide for colonies and displays colony data
     * pre: none
     * post: Colony data has been displayed.
     */
    public void displayColonies() {
        int count;
        System.out.format("%-10s %-10s %-10s" , "LOCATION" , "SIZE" , "COLOUR");
        System.out.println();

        for (int row = 0; row < slideData.length; row++) {
            for (int col = 0; col < slideData[0].length; col++) {
                for (int i = 1; i <= 9; i++) {
                    if (slideData[row][col] == i) {
                        count = collectCells(row , col , (char)i);
                        System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
                    }
                }
            }
        }
    }


    /**
     * Displays a slide.
     * pre: none
     * post: Slide data has been displayed.
     */
    public void displaySlide() {

        for (int row = 0; row < slideData.length; row++) {
            for (int col = 0; col < slideData[0].length; col++) {
                System.out.print(slideData[row][col]);
            }
            System.out.println();
        }
    }
}

You are comparing an int (the value in i ) (eg 3) against characters (like '1', '2'...) (the values in slideData ) which as integers are values starting at 0x30 ('0')..您正在将 int( i中的值)(例如 3)与字符(如 '1'、'2'...)( slideData中的值)进行比较,其中整数是从 0x30('0')开始的值。 .

So without rewriting your program the simplest solution to fix your incompatible types by converting your for loop value (1..9) to the character equivalent as in the following:因此,无需重写您的程序,通过将 for 循环值 (1..9) 转换为等价字符来修复不兼容类型的最简单解决方案,如下所示:

for (int i = 1; i <= 9; i++)
{
    char x = (char) (0x30 + i);
    if (slideData[row][col] == x)
    {
       // ...
    }
}

To convert the integer (digit) to a char you could also do:要将 integer (数字)转换为char ,您还可以执行以下操作:

  char x = Character.forDigit(i, 10);

Change改变

col >= slideData[0].length

to

col >= slideData[row].length

inside method private int collectCells(int row, int col, char colour)内部方法private int collectCells(int row, int col, char colour)

As others also pointed out - you are comparing a char (character) to an int (integer).正如其他人还指出的那样 - 您正在将 char (字符)与 int (整数)进行比较。 I can be a little confusing when you see that the value in your array slideData at cell [0][0] (row=0 and col=0) contains 1, the value in i is also 1 but the condition fails.当您看到单元格 [0][0](行 = 0 和 col = 0)处的数组slideData中的值包含 1, i中的值也是 1 但条件失败时,我可能会有点困惑。 But there's a difference between what is stored in your array of characters and the value of an integer variable.但是存储在characters数组中的内容与 integer 变量的值之间存在差异。

In Java when you compare a character to an integer the JVM uses the character's ASCII value.在 Java 中,当您将characterinteger进行比较时,JVM 使用字符的 ASCII 值。 it means the value 1,= '1'.这意味着值 1,= '1'。 since the ASCII value of '1' is 49.因为 '1' 的 ASCII 值是 49。

it's possible you don't see the difference because of how your IDE's debugger shows the values of your character array.由于 IDE 的调试器如何显示字符数组的值,您可能看不到差异。 In IntelliJ, for example, you will see: ![slideData[0][0] value and ASCII value] 1 It shows you that the value in slideData[0][0] is '1' (character value) and its ASCII (integer) value is 49. Your condition will work if you make a small change to it:例如,在 IntelliJ 中,您将看到: ![slideData[0][0] value and ASCII value] 1它显示slideData[0][0]中的值是 '1'(字符值)及其 ASCII (整数)值为 49。如果您对其进行小幅更改,您的条件将起作用:

if (slideData[row][col] == 48 + i) {
   count = collectCells(row , col , (char)i);
   System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
}

The integer value 48 is the ASCII value of '0' character, adding the value of i to it will give you the ASCII value of the character you are comparing. integer 值 48 是 '0' 字符的 ASCII 值,将i的值添加到它会给您正在比较的字符的 ASCII 值。

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

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