[英]Why won't the else statement in my while loop execute when the conditions in the if statement aren't met?
[英]Why won't my array satisfy the conditions of this if-statement?
我正在做这个程序来进行分配,我完全零知道为什么我的数组(从单独的文件中读取值)不满足这个 if 语句的条件。 为方便起见,有问题的语句位于 displayColonies class 中。
当我读入我的数组时,它会在指定的行和列中查找值,这是一个从 1-9 注册的 int 值。 当我使用打印语句定期对此进行测试时,数组确实包含正确的值,但 if 语句从未被激活。 条件与数组的值匹配,但从不评估语句,无论是否为true
。
谢谢你的帮助。
下面附上代码: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();
}
}
}
您正在将 int( i
中的值)(例如 3)与字符(如 '1'、'2'...)( slideData
中的值)进行比较,其中整数是从 0x30('0')开始的值。 .
因此,无需重写您的程序,通过将 for 循环值 (1..9) 转换为等价字符来修复不兼容类型的最简单解决方案,如下所示:
for (int i = 1; i <= 9; i++)
{
char x = (char) (0x30 + i);
if (slideData[row][col] == x)
{
// ...
}
}
要将 integer (数字)转换为char
,您还可以执行以下操作:
char x = Character.forDigit(i, 10);
改变
col >= slideData[0].length
至
col >= slideData[row].length
内部方法private int collectCells(int row, int col, char colour)
正如其他人还指出的那样 - 您正在将 char (字符)与 int (整数)进行比较。 当您看到单元格 [0][0](行 = 0 和 col = 0)处的数组slideData
中的值包含 1, i
中的值也是 1 但条件失败时,我可能会有点困惑。 但是存储在characters
数组中的内容与 integer 变量的值之间存在差异。
在 Java 中,当您将character
与integer
进行比较时,JVM 使用字符的 ASCII 值。 这意味着值 1,= '1'。 因为 '1' 的 ASCII 值是 49。
由于 IDE 的调试器如何显示字符数组的值,您可能看不到差异。 例如,在 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);
}
integer 值 48 是 '0' 字符的 ASCII 值,将i
的值添加到它会给您正在比较的字符的 ASCII 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.