我正在为分配该程序,而我绝对不知道为什么我的数组(从单独的文件中读取值)不能满足此if语句的条件。 为方便起见,有问题的语句位于displayColonies类中。
当我读入数组时,它会在指定的行和列中查找值,这是从1到9范围内的int值。当我使用print语句定期测试时,该数组确实包含正确的值,但是if语句从未激活。条件与数组的值匹配,但不会对语句进行true
或其他方式的评估。
感谢您的帮助。
代码附在下面: DetectColonies2ElectricBoogaloo是客户端 幻灯片是对象 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();
}
}
}
答案 0 :(得分:2)
您正在将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)
{
// ...
}
}
要将整数(数字)转换为char
,您还可以执行以下操作:
char x = Character.forDigit(i, 10);
答案 1 :(得分:0)
正如其他人也指出的那样-您正在将char(字符)与int(整数)进行比较。当您看到数组slideData
中单元格[0] [0](行= 0和col = 0)的值包含1时,我可能会有些困惑1,但条件失败。但是i
数组中存储的内容和整数变量的值之间是有区别的。
在Java中,当您将characters
与character
进行比较时,JVM将使用字符的ASCII值。这表示值1!='1',因为ASCII值'1'为49。
由于IDE的调试器如何显示字符数组的值,您可能看不到差异。例如,在IntelliJ中,您将看到:
![slideData[0][0] value and ASCII value] 1
它显示integer
中的值为'1'(字符值),其ASCII(整数)值为49。
如果您对其进行较小的更改,您的条件将起作用:
slideData[0][0]
整数值48是'0'字符的ASCII值,在其上加上if (slideData[row][col] == 48 + i) {
count = collectCells(row , col , (char)i);
System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
}
的值将得到要比较的字符的ASCII值。